@longleash/cli 0.1.0-rc.1 → 0.1.0-rc.4
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 +6 -3
- package/bin/longleash.mjs +1790 -42
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -1
- package/runtime/app/dist/assets/{app-CxamvvoS.js → app-DvraQaIc.js} +2 -2
- package/runtime/app/dist/assets/welcome-CTTvis4A.js +31 -0
- package/runtime/app/dist/build.json +1 -1
- package/runtime/app/dist/index.html +1 -1
- package/runtime/app/dist/welcome.html +1 -1
- package/runtime/daemon/bin/longleashd.mjs +153 -28
- package/runtime/app/dist/assets/welcome-CG_rEa--.js +0 -27
package/bin/longleash.mjs
CHANGED
|
@@ -1,11 +1,1106 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
|
|
28
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRMode.js
|
|
29
|
+
var require_QRMode = __commonJS({
|
|
30
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRMode.js"(exports, module) {
|
|
31
|
+
module.exports = {
|
|
32
|
+
MODE_NUMBER: 1 << 0,
|
|
33
|
+
MODE_ALPHA_NUM: 1 << 1,
|
|
34
|
+
MODE_8BIT_BYTE: 1 << 2,
|
|
35
|
+
MODE_KANJI: 1 << 3
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QR8bitByte.js
|
|
41
|
+
var require_QR8bitByte = __commonJS({
|
|
42
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QR8bitByte.js"(exports, module) {
|
|
43
|
+
var QRMode = require_QRMode();
|
|
44
|
+
function QR8bitByte(data) {
|
|
45
|
+
this.mode = QRMode.MODE_8BIT_BYTE;
|
|
46
|
+
this.data = data;
|
|
47
|
+
}
|
|
48
|
+
QR8bitByte.prototype = {
|
|
49
|
+
getLength: function() {
|
|
50
|
+
return this.data.length;
|
|
51
|
+
},
|
|
52
|
+
write: function(buffer) {
|
|
53
|
+
for (var i = 0; i < this.data.length; i++) {
|
|
54
|
+
buffer.put(this.data.charCodeAt(i), 8);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
module.exports = QR8bitByte;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRMath.js
|
|
63
|
+
var require_QRMath = __commonJS({
|
|
64
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRMath.js"(exports, module) {
|
|
65
|
+
var QRMath = {
|
|
66
|
+
glog: function(n) {
|
|
67
|
+
if (n < 1) {
|
|
68
|
+
throw new Error("glog(" + n + ")");
|
|
69
|
+
}
|
|
70
|
+
return QRMath.LOG_TABLE[n];
|
|
71
|
+
},
|
|
72
|
+
gexp: function(n) {
|
|
73
|
+
while (n < 0) {
|
|
74
|
+
n += 255;
|
|
75
|
+
}
|
|
76
|
+
while (n >= 256) {
|
|
77
|
+
n -= 255;
|
|
78
|
+
}
|
|
79
|
+
return QRMath.EXP_TABLE[n];
|
|
80
|
+
},
|
|
81
|
+
EXP_TABLE: new Array(256),
|
|
82
|
+
LOG_TABLE: new Array(256)
|
|
83
|
+
};
|
|
84
|
+
for (i = 0; i < 8; i++) {
|
|
85
|
+
QRMath.EXP_TABLE[i] = 1 << i;
|
|
86
|
+
}
|
|
87
|
+
var i;
|
|
88
|
+
for (i = 8; i < 256; i++) {
|
|
89
|
+
QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8];
|
|
90
|
+
}
|
|
91
|
+
var i;
|
|
92
|
+
for (i = 0; i < 255; i++) {
|
|
93
|
+
QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
|
|
94
|
+
}
|
|
95
|
+
var i;
|
|
96
|
+
module.exports = QRMath;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRPolynomial.js
|
|
101
|
+
var require_QRPolynomial = __commonJS({
|
|
102
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRPolynomial.js"(exports, module) {
|
|
103
|
+
var QRMath = require_QRMath();
|
|
104
|
+
function QRPolynomial(num, shift) {
|
|
105
|
+
if (num.length === void 0) {
|
|
106
|
+
throw new Error(num.length + "/" + shift);
|
|
107
|
+
}
|
|
108
|
+
var offset = 0;
|
|
109
|
+
while (offset < num.length && num[offset] === 0) {
|
|
110
|
+
offset++;
|
|
111
|
+
}
|
|
112
|
+
this.num = new Array(num.length - offset + shift);
|
|
113
|
+
for (var i = 0; i < num.length - offset; i++) {
|
|
114
|
+
this.num[i] = num[i + offset];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
QRPolynomial.prototype = {
|
|
118
|
+
get: function(index) {
|
|
119
|
+
return this.num[index];
|
|
120
|
+
},
|
|
121
|
+
getLength: function() {
|
|
122
|
+
return this.num.length;
|
|
123
|
+
},
|
|
124
|
+
multiply: function(e) {
|
|
125
|
+
var num = new Array(this.getLength() + e.getLength() - 1);
|
|
126
|
+
for (var i = 0; i < this.getLength(); i++) {
|
|
127
|
+
for (var j = 0; j < e.getLength(); j++) {
|
|
128
|
+
num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return new QRPolynomial(num, 0);
|
|
132
|
+
},
|
|
133
|
+
mod: function(e) {
|
|
134
|
+
if (this.getLength() - e.getLength() < 0) {
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
var ratio = QRMath.glog(this.get(0)) - QRMath.glog(e.get(0));
|
|
138
|
+
var num = new Array(this.getLength());
|
|
139
|
+
for (var i = 0; i < this.getLength(); i++) {
|
|
140
|
+
num[i] = this.get(i);
|
|
141
|
+
}
|
|
142
|
+
for (var x = 0; x < e.getLength(); x++) {
|
|
143
|
+
num[x] ^= QRMath.gexp(QRMath.glog(e.get(x)) + ratio);
|
|
144
|
+
}
|
|
145
|
+
return new QRPolynomial(num, 0).mod(e);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
module.exports = QRPolynomial;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRMaskPattern.js
|
|
153
|
+
var require_QRMaskPattern = __commonJS({
|
|
154
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRMaskPattern.js"(exports, module) {
|
|
155
|
+
module.exports = {
|
|
156
|
+
PATTERN000: 0,
|
|
157
|
+
PATTERN001: 1,
|
|
158
|
+
PATTERN010: 2,
|
|
159
|
+
PATTERN011: 3,
|
|
160
|
+
PATTERN100: 4,
|
|
161
|
+
PATTERN101: 5,
|
|
162
|
+
PATTERN110: 6,
|
|
163
|
+
PATTERN111: 7
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRUtil.js
|
|
169
|
+
var require_QRUtil = __commonJS({
|
|
170
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRUtil.js"(exports, module) {
|
|
171
|
+
var QRMode = require_QRMode();
|
|
172
|
+
var QRPolynomial = require_QRPolynomial();
|
|
173
|
+
var QRMath = require_QRMath();
|
|
174
|
+
var QRMaskPattern = require_QRMaskPattern();
|
|
175
|
+
var QRUtil = {
|
|
176
|
+
PATTERN_POSITION_TABLE: [
|
|
177
|
+
[],
|
|
178
|
+
[6, 18],
|
|
179
|
+
[6, 22],
|
|
180
|
+
[6, 26],
|
|
181
|
+
[6, 30],
|
|
182
|
+
[6, 34],
|
|
183
|
+
[6, 22, 38],
|
|
184
|
+
[6, 24, 42],
|
|
185
|
+
[6, 26, 46],
|
|
186
|
+
[6, 28, 50],
|
|
187
|
+
[6, 30, 54],
|
|
188
|
+
[6, 32, 58],
|
|
189
|
+
[6, 34, 62],
|
|
190
|
+
[6, 26, 46, 66],
|
|
191
|
+
[6, 26, 48, 70],
|
|
192
|
+
[6, 26, 50, 74],
|
|
193
|
+
[6, 30, 54, 78],
|
|
194
|
+
[6, 30, 56, 82],
|
|
195
|
+
[6, 30, 58, 86],
|
|
196
|
+
[6, 34, 62, 90],
|
|
197
|
+
[6, 28, 50, 72, 94],
|
|
198
|
+
[6, 26, 50, 74, 98],
|
|
199
|
+
[6, 30, 54, 78, 102],
|
|
200
|
+
[6, 28, 54, 80, 106],
|
|
201
|
+
[6, 32, 58, 84, 110],
|
|
202
|
+
[6, 30, 58, 86, 114],
|
|
203
|
+
[6, 34, 62, 90, 118],
|
|
204
|
+
[6, 26, 50, 74, 98, 122],
|
|
205
|
+
[6, 30, 54, 78, 102, 126],
|
|
206
|
+
[6, 26, 52, 78, 104, 130],
|
|
207
|
+
[6, 30, 56, 82, 108, 134],
|
|
208
|
+
[6, 34, 60, 86, 112, 138],
|
|
209
|
+
[6, 30, 58, 86, 114, 142],
|
|
210
|
+
[6, 34, 62, 90, 118, 146],
|
|
211
|
+
[6, 30, 54, 78, 102, 126, 150],
|
|
212
|
+
[6, 24, 50, 76, 102, 128, 154],
|
|
213
|
+
[6, 28, 54, 80, 106, 132, 158],
|
|
214
|
+
[6, 32, 58, 84, 110, 136, 162],
|
|
215
|
+
[6, 26, 54, 82, 110, 138, 166],
|
|
216
|
+
[6, 30, 58, 86, 114, 142, 170]
|
|
217
|
+
],
|
|
218
|
+
G15: 1 << 10 | 1 << 8 | 1 << 5 | 1 << 4 | 1 << 2 | 1 << 1 | 1 << 0,
|
|
219
|
+
G18: 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0,
|
|
220
|
+
G15_MASK: 1 << 14 | 1 << 12 | 1 << 10 | 1 << 4 | 1 << 1,
|
|
221
|
+
getBCHTypeInfo: function(data) {
|
|
222
|
+
var d = data << 10;
|
|
223
|
+
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
|
|
224
|
+
d ^= QRUtil.G15 << QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15);
|
|
225
|
+
}
|
|
226
|
+
return (data << 10 | d) ^ QRUtil.G15_MASK;
|
|
227
|
+
},
|
|
228
|
+
getBCHTypeNumber: function(data) {
|
|
229
|
+
var d = data << 12;
|
|
230
|
+
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
|
|
231
|
+
d ^= QRUtil.G18 << QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18);
|
|
232
|
+
}
|
|
233
|
+
return data << 12 | d;
|
|
234
|
+
},
|
|
235
|
+
getBCHDigit: function(data) {
|
|
236
|
+
var digit = 0;
|
|
237
|
+
while (data !== 0) {
|
|
238
|
+
digit++;
|
|
239
|
+
data >>>= 1;
|
|
240
|
+
}
|
|
241
|
+
return digit;
|
|
242
|
+
},
|
|
243
|
+
getPatternPosition: function(typeNumber) {
|
|
244
|
+
return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
|
|
245
|
+
},
|
|
246
|
+
getMask: function(maskPattern, i, j) {
|
|
247
|
+
switch (maskPattern) {
|
|
248
|
+
case QRMaskPattern.PATTERN000:
|
|
249
|
+
return (i + j) % 2 === 0;
|
|
250
|
+
case QRMaskPattern.PATTERN001:
|
|
251
|
+
return i % 2 === 0;
|
|
252
|
+
case QRMaskPattern.PATTERN010:
|
|
253
|
+
return j % 3 === 0;
|
|
254
|
+
case QRMaskPattern.PATTERN011:
|
|
255
|
+
return (i + j) % 3 === 0;
|
|
256
|
+
case QRMaskPattern.PATTERN100:
|
|
257
|
+
return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0;
|
|
258
|
+
case QRMaskPattern.PATTERN101:
|
|
259
|
+
return i * j % 2 + i * j % 3 === 0;
|
|
260
|
+
case QRMaskPattern.PATTERN110:
|
|
261
|
+
return (i * j % 2 + i * j % 3) % 2 === 0;
|
|
262
|
+
case QRMaskPattern.PATTERN111:
|
|
263
|
+
return (i * j % 3 + (i + j) % 2) % 2 === 0;
|
|
264
|
+
default:
|
|
265
|
+
throw new Error("bad maskPattern:" + maskPattern);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
getErrorCorrectPolynomial: function(errorCorrectLength) {
|
|
269
|
+
var a = new QRPolynomial([1], 0);
|
|
270
|
+
for (var i = 0; i < errorCorrectLength; i++) {
|
|
271
|
+
a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
|
|
272
|
+
}
|
|
273
|
+
return a;
|
|
274
|
+
},
|
|
275
|
+
getLengthInBits: function(mode, type) {
|
|
276
|
+
if (1 <= type && type < 10) {
|
|
277
|
+
switch (mode) {
|
|
278
|
+
case QRMode.MODE_NUMBER:
|
|
279
|
+
return 10;
|
|
280
|
+
case QRMode.MODE_ALPHA_NUM:
|
|
281
|
+
return 9;
|
|
282
|
+
case QRMode.MODE_8BIT_BYTE:
|
|
283
|
+
return 8;
|
|
284
|
+
case QRMode.MODE_KANJI:
|
|
285
|
+
return 8;
|
|
286
|
+
default:
|
|
287
|
+
throw new Error("mode:" + mode);
|
|
288
|
+
}
|
|
289
|
+
} else if (type < 27) {
|
|
290
|
+
switch (mode) {
|
|
291
|
+
case QRMode.MODE_NUMBER:
|
|
292
|
+
return 12;
|
|
293
|
+
case QRMode.MODE_ALPHA_NUM:
|
|
294
|
+
return 11;
|
|
295
|
+
case QRMode.MODE_8BIT_BYTE:
|
|
296
|
+
return 16;
|
|
297
|
+
case QRMode.MODE_KANJI:
|
|
298
|
+
return 10;
|
|
299
|
+
default:
|
|
300
|
+
throw new Error("mode:" + mode);
|
|
301
|
+
}
|
|
302
|
+
} else if (type < 41) {
|
|
303
|
+
switch (mode) {
|
|
304
|
+
case QRMode.MODE_NUMBER:
|
|
305
|
+
return 14;
|
|
306
|
+
case QRMode.MODE_ALPHA_NUM:
|
|
307
|
+
return 13;
|
|
308
|
+
case QRMode.MODE_8BIT_BYTE:
|
|
309
|
+
return 16;
|
|
310
|
+
case QRMode.MODE_KANJI:
|
|
311
|
+
return 12;
|
|
312
|
+
default:
|
|
313
|
+
throw new Error("mode:" + mode);
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
throw new Error("type:" + type);
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
getLostPoint: function(qrCode) {
|
|
320
|
+
var moduleCount = qrCode.getModuleCount();
|
|
321
|
+
var lostPoint = 0;
|
|
322
|
+
var row = 0;
|
|
323
|
+
var col = 0;
|
|
324
|
+
for (row = 0; row < moduleCount; row++) {
|
|
325
|
+
for (col = 0; col < moduleCount; col++) {
|
|
326
|
+
var sameCount = 0;
|
|
327
|
+
var dark = qrCode.isDark(row, col);
|
|
328
|
+
for (var r = -1; r <= 1; r++) {
|
|
329
|
+
if (row + r < 0 || moduleCount <= row + r) {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
for (var c = -1; c <= 1; c++) {
|
|
333
|
+
if (col + c < 0 || moduleCount <= col + c) {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (r === 0 && c === 0) {
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
if (dark === qrCode.isDark(row + r, col + c)) {
|
|
340
|
+
sameCount++;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (sameCount > 5) {
|
|
345
|
+
lostPoint += 3 + sameCount - 5;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
for (row = 0; row < moduleCount - 1; row++) {
|
|
350
|
+
for (col = 0; col < moduleCount - 1; col++) {
|
|
351
|
+
var count = 0;
|
|
352
|
+
if (qrCode.isDark(row, col)) count++;
|
|
353
|
+
if (qrCode.isDark(row + 1, col)) count++;
|
|
354
|
+
if (qrCode.isDark(row, col + 1)) count++;
|
|
355
|
+
if (qrCode.isDark(row + 1, col + 1)) count++;
|
|
356
|
+
if (count === 0 || count === 4) {
|
|
357
|
+
lostPoint += 3;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
for (row = 0; row < moduleCount; row++) {
|
|
362
|
+
for (col = 0; col < moduleCount - 6; col++) {
|
|
363
|
+
if (qrCode.isDark(row, col) && !qrCode.isDark(row, col + 1) && qrCode.isDark(row, col + 2) && qrCode.isDark(row, col + 3) && qrCode.isDark(row, col + 4) && !qrCode.isDark(row, col + 5) && qrCode.isDark(row, col + 6)) {
|
|
364
|
+
lostPoint += 40;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (col = 0; col < moduleCount; col++) {
|
|
369
|
+
for (row = 0; row < moduleCount - 6; row++) {
|
|
370
|
+
if (qrCode.isDark(row, col) && !qrCode.isDark(row + 1, col) && qrCode.isDark(row + 2, col) && qrCode.isDark(row + 3, col) && qrCode.isDark(row + 4, col) && !qrCode.isDark(row + 5, col) && qrCode.isDark(row + 6, col)) {
|
|
371
|
+
lostPoint += 40;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
var darkCount = 0;
|
|
376
|
+
for (col = 0; col < moduleCount; col++) {
|
|
377
|
+
for (row = 0; row < moduleCount; row++) {
|
|
378
|
+
if (qrCode.isDark(row, col)) {
|
|
379
|
+
darkCount++;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
|
|
384
|
+
lostPoint += ratio * 10;
|
|
385
|
+
return lostPoint;
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
module.exports = QRUtil;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRErrorCorrectLevel.js
|
|
393
|
+
var require_QRErrorCorrectLevel = __commonJS({
|
|
394
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRErrorCorrectLevel.js"(exports, module) {
|
|
395
|
+
module.exports = {
|
|
396
|
+
L: 1,
|
|
397
|
+
M: 0,
|
|
398
|
+
Q: 3,
|
|
399
|
+
H: 2
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRRSBlock.js
|
|
405
|
+
var require_QRRSBlock = __commonJS({
|
|
406
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRRSBlock.js"(exports, module) {
|
|
407
|
+
var QRErrorCorrectLevel = require_QRErrorCorrectLevel();
|
|
408
|
+
function QRRSBlock(totalCount, dataCount) {
|
|
409
|
+
this.totalCount = totalCount;
|
|
410
|
+
this.dataCount = dataCount;
|
|
411
|
+
}
|
|
412
|
+
QRRSBlock.RS_BLOCK_TABLE = [
|
|
413
|
+
// L
|
|
414
|
+
// M
|
|
415
|
+
// Q
|
|
416
|
+
// H
|
|
417
|
+
// 1
|
|
418
|
+
[1, 26, 19],
|
|
419
|
+
[1, 26, 16],
|
|
420
|
+
[1, 26, 13],
|
|
421
|
+
[1, 26, 9],
|
|
422
|
+
// 2
|
|
423
|
+
[1, 44, 34],
|
|
424
|
+
[1, 44, 28],
|
|
425
|
+
[1, 44, 22],
|
|
426
|
+
[1, 44, 16],
|
|
427
|
+
// 3
|
|
428
|
+
[1, 70, 55],
|
|
429
|
+
[1, 70, 44],
|
|
430
|
+
[2, 35, 17],
|
|
431
|
+
[2, 35, 13],
|
|
432
|
+
// 4
|
|
433
|
+
[1, 100, 80],
|
|
434
|
+
[2, 50, 32],
|
|
435
|
+
[2, 50, 24],
|
|
436
|
+
[4, 25, 9],
|
|
437
|
+
// 5
|
|
438
|
+
[1, 134, 108],
|
|
439
|
+
[2, 67, 43],
|
|
440
|
+
[2, 33, 15, 2, 34, 16],
|
|
441
|
+
[2, 33, 11, 2, 34, 12],
|
|
442
|
+
// 6
|
|
443
|
+
[2, 86, 68],
|
|
444
|
+
[4, 43, 27],
|
|
445
|
+
[4, 43, 19],
|
|
446
|
+
[4, 43, 15],
|
|
447
|
+
// 7
|
|
448
|
+
[2, 98, 78],
|
|
449
|
+
[4, 49, 31],
|
|
450
|
+
[2, 32, 14, 4, 33, 15],
|
|
451
|
+
[4, 39, 13, 1, 40, 14],
|
|
452
|
+
// 8
|
|
453
|
+
[2, 121, 97],
|
|
454
|
+
[2, 60, 38, 2, 61, 39],
|
|
455
|
+
[4, 40, 18, 2, 41, 19],
|
|
456
|
+
[4, 40, 14, 2, 41, 15],
|
|
457
|
+
// 9
|
|
458
|
+
[2, 146, 116],
|
|
459
|
+
[3, 58, 36, 2, 59, 37],
|
|
460
|
+
[4, 36, 16, 4, 37, 17],
|
|
461
|
+
[4, 36, 12, 4, 37, 13],
|
|
462
|
+
// 10
|
|
463
|
+
[2, 86, 68, 2, 87, 69],
|
|
464
|
+
[4, 69, 43, 1, 70, 44],
|
|
465
|
+
[6, 43, 19, 2, 44, 20],
|
|
466
|
+
[6, 43, 15, 2, 44, 16],
|
|
467
|
+
// 11
|
|
468
|
+
[4, 101, 81],
|
|
469
|
+
[1, 80, 50, 4, 81, 51],
|
|
470
|
+
[4, 50, 22, 4, 51, 23],
|
|
471
|
+
[3, 36, 12, 8, 37, 13],
|
|
472
|
+
// 12
|
|
473
|
+
[2, 116, 92, 2, 117, 93],
|
|
474
|
+
[6, 58, 36, 2, 59, 37],
|
|
475
|
+
[4, 46, 20, 6, 47, 21],
|
|
476
|
+
[7, 42, 14, 4, 43, 15],
|
|
477
|
+
// 13
|
|
478
|
+
[4, 133, 107],
|
|
479
|
+
[8, 59, 37, 1, 60, 38],
|
|
480
|
+
[8, 44, 20, 4, 45, 21],
|
|
481
|
+
[12, 33, 11, 4, 34, 12],
|
|
482
|
+
// 14
|
|
483
|
+
[3, 145, 115, 1, 146, 116],
|
|
484
|
+
[4, 64, 40, 5, 65, 41],
|
|
485
|
+
[11, 36, 16, 5, 37, 17],
|
|
486
|
+
[11, 36, 12, 5, 37, 13],
|
|
487
|
+
// 15
|
|
488
|
+
[5, 109, 87, 1, 110, 88],
|
|
489
|
+
[5, 65, 41, 5, 66, 42],
|
|
490
|
+
[5, 54, 24, 7, 55, 25],
|
|
491
|
+
[11, 36, 12],
|
|
492
|
+
// 16
|
|
493
|
+
[5, 122, 98, 1, 123, 99],
|
|
494
|
+
[7, 73, 45, 3, 74, 46],
|
|
495
|
+
[15, 43, 19, 2, 44, 20],
|
|
496
|
+
[3, 45, 15, 13, 46, 16],
|
|
497
|
+
// 17
|
|
498
|
+
[1, 135, 107, 5, 136, 108],
|
|
499
|
+
[10, 74, 46, 1, 75, 47],
|
|
500
|
+
[1, 50, 22, 15, 51, 23],
|
|
501
|
+
[2, 42, 14, 17, 43, 15],
|
|
502
|
+
// 18
|
|
503
|
+
[5, 150, 120, 1, 151, 121],
|
|
504
|
+
[9, 69, 43, 4, 70, 44],
|
|
505
|
+
[17, 50, 22, 1, 51, 23],
|
|
506
|
+
[2, 42, 14, 19, 43, 15],
|
|
507
|
+
// 19
|
|
508
|
+
[3, 141, 113, 4, 142, 114],
|
|
509
|
+
[3, 70, 44, 11, 71, 45],
|
|
510
|
+
[17, 47, 21, 4, 48, 22],
|
|
511
|
+
[9, 39, 13, 16, 40, 14],
|
|
512
|
+
// 20
|
|
513
|
+
[3, 135, 107, 5, 136, 108],
|
|
514
|
+
[3, 67, 41, 13, 68, 42],
|
|
515
|
+
[15, 54, 24, 5, 55, 25],
|
|
516
|
+
[15, 43, 15, 10, 44, 16],
|
|
517
|
+
// 21
|
|
518
|
+
[4, 144, 116, 4, 145, 117],
|
|
519
|
+
[17, 68, 42],
|
|
520
|
+
[17, 50, 22, 6, 51, 23],
|
|
521
|
+
[19, 46, 16, 6, 47, 17],
|
|
522
|
+
// 22
|
|
523
|
+
[2, 139, 111, 7, 140, 112],
|
|
524
|
+
[17, 74, 46],
|
|
525
|
+
[7, 54, 24, 16, 55, 25],
|
|
526
|
+
[34, 37, 13],
|
|
527
|
+
// 23
|
|
528
|
+
[4, 151, 121, 5, 152, 122],
|
|
529
|
+
[4, 75, 47, 14, 76, 48],
|
|
530
|
+
[11, 54, 24, 14, 55, 25],
|
|
531
|
+
[16, 45, 15, 14, 46, 16],
|
|
532
|
+
// 24
|
|
533
|
+
[6, 147, 117, 4, 148, 118],
|
|
534
|
+
[6, 73, 45, 14, 74, 46],
|
|
535
|
+
[11, 54, 24, 16, 55, 25],
|
|
536
|
+
[30, 46, 16, 2, 47, 17],
|
|
537
|
+
// 25
|
|
538
|
+
[8, 132, 106, 4, 133, 107],
|
|
539
|
+
[8, 75, 47, 13, 76, 48],
|
|
540
|
+
[7, 54, 24, 22, 55, 25],
|
|
541
|
+
[22, 45, 15, 13, 46, 16],
|
|
542
|
+
// 26
|
|
543
|
+
[10, 142, 114, 2, 143, 115],
|
|
544
|
+
[19, 74, 46, 4, 75, 47],
|
|
545
|
+
[28, 50, 22, 6, 51, 23],
|
|
546
|
+
[33, 46, 16, 4, 47, 17],
|
|
547
|
+
// 27
|
|
548
|
+
[8, 152, 122, 4, 153, 123],
|
|
549
|
+
[22, 73, 45, 3, 74, 46],
|
|
550
|
+
[8, 53, 23, 26, 54, 24],
|
|
551
|
+
[12, 45, 15, 28, 46, 16],
|
|
552
|
+
// 28
|
|
553
|
+
[3, 147, 117, 10, 148, 118],
|
|
554
|
+
[3, 73, 45, 23, 74, 46],
|
|
555
|
+
[4, 54, 24, 31, 55, 25],
|
|
556
|
+
[11, 45, 15, 31, 46, 16],
|
|
557
|
+
// 29
|
|
558
|
+
[7, 146, 116, 7, 147, 117],
|
|
559
|
+
[21, 73, 45, 7, 74, 46],
|
|
560
|
+
[1, 53, 23, 37, 54, 24],
|
|
561
|
+
[19, 45, 15, 26, 46, 16],
|
|
562
|
+
// 30
|
|
563
|
+
[5, 145, 115, 10, 146, 116],
|
|
564
|
+
[19, 75, 47, 10, 76, 48],
|
|
565
|
+
[15, 54, 24, 25, 55, 25],
|
|
566
|
+
[23, 45, 15, 25, 46, 16],
|
|
567
|
+
// 31
|
|
568
|
+
[13, 145, 115, 3, 146, 116],
|
|
569
|
+
[2, 74, 46, 29, 75, 47],
|
|
570
|
+
[42, 54, 24, 1, 55, 25],
|
|
571
|
+
[23, 45, 15, 28, 46, 16],
|
|
572
|
+
// 32
|
|
573
|
+
[17, 145, 115],
|
|
574
|
+
[10, 74, 46, 23, 75, 47],
|
|
575
|
+
[10, 54, 24, 35, 55, 25],
|
|
576
|
+
[19, 45, 15, 35, 46, 16],
|
|
577
|
+
// 33
|
|
578
|
+
[17, 145, 115, 1, 146, 116],
|
|
579
|
+
[14, 74, 46, 21, 75, 47],
|
|
580
|
+
[29, 54, 24, 19, 55, 25],
|
|
581
|
+
[11, 45, 15, 46, 46, 16],
|
|
582
|
+
// 34
|
|
583
|
+
[13, 145, 115, 6, 146, 116],
|
|
584
|
+
[14, 74, 46, 23, 75, 47],
|
|
585
|
+
[44, 54, 24, 7, 55, 25],
|
|
586
|
+
[59, 46, 16, 1, 47, 17],
|
|
587
|
+
// 35
|
|
588
|
+
[12, 151, 121, 7, 152, 122],
|
|
589
|
+
[12, 75, 47, 26, 76, 48],
|
|
590
|
+
[39, 54, 24, 14, 55, 25],
|
|
591
|
+
[22, 45, 15, 41, 46, 16],
|
|
592
|
+
// 36
|
|
593
|
+
[6, 151, 121, 14, 152, 122],
|
|
594
|
+
[6, 75, 47, 34, 76, 48],
|
|
595
|
+
[46, 54, 24, 10, 55, 25],
|
|
596
|
+
[2, 45, 15, 64, 46, 16],
|
|
597
|
+
// 37
|
|
598
|
+
[17, 152, 122, 4, 153, 123],
|
|
599
|
+
[29, 74, 46, 14, 75, 47],
|
|
600
|
+
[49, 54, 24, 10, 55, 25],
|
|
601
|
+
[24, 45, 15, 46, 46, 16],
|
|
602
|
+
// 38
|
|
603
|
+
[4, 152, 122, 18, 153, 123],
|
|
604
|
+
[13, 74, 46, 32, 75, 47],
|
|
605
|
+
[48, 54, 24, 14, 55, 25],
|
|
606
|
+
[42, 45, 15, 32, 46, 16],
|
|
607
|
+
// 39
|
|
608
|
+
[20, 147, 117, 4, 148, 118],
|
|
609
|
+
[40, 75, 47, 7, 76, 48],
|
|
610
|
+
[43, 54, 24, 22, 55, 25],
|
|
611
|
+
[10, 45, 15, 67, 46, 16],
|
|
612
|
+
// 40
|
|
613
|
+
[19, 148, 118, 6, 149, 119],
|
|
614
|
+
[18, 75, 47, 31, 76, 48],
|
|
615
|
+
[34, 54, 24, 34, 55, 25],
|
|
616
|
+
[20, 45, 15, 61, 46, 16]
|
|
617
|
+
];
|
|
618
|
+
QRRSBlock.getRSBlocks = function(typeNumber, errorCorrectLevel) {
|
|
619
|
+
var rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel);
|
|
620
|
+
if (rsBlock === void 0) {
|
|
621
|
+
throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + errorCorrectLevel);
|
|
622
|
+
}
|
|
623
|
+
var length = rsBlock.length / 3;
|
|
624
|
+
var list = [];
|
|
625
|
+
for (var i = 0; i < length; i++) {
|
|
626
|
+
var count = rsBlock[i * 3 + 0];
|
|
627
|
+
var totalCount = rsBlock[i * 3 + 1];
|
|
628
|
+
var dataCount = rsBlock[i * 3 + 2];
|
|
629
|
+
for (var j = 0; j < count; j++) {
|
|
630
|
+
list.push(new QRRSBlock(totalCount, dataCount));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return list;
|
|
634
|
+
};
|
|
635
|
+
QRRSBlock.getRsBlockTable = function(typeNumber, errorCorrectLevel) {
|
|
636
|
+
switch (errorCorrectLevel) {
|
|
637
|
+
case QRErrorCorrectLevel.L:
|
|
638
|
+
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
|
|
639
|
+
case QRErrorCorrectLevel.M:
|
|
640
|
+
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
|
|
641
|
+
case QRErrorCorrectLevel.Q:
|
|
642
|
+
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
|
|
643
|
+
case QRErrorCorrectLevel.H:
|
|
644
|
+
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
|
|
645
|
+
default:
|
|
646
|
+
return void 0;
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
module.exports = QRRSBlock;
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRBitBuffer.js
|
|
654
|
+
var require_QRBitBuffer = __commonJS({
|
|
655
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/QRBitBuffer.js"(exports, module) {
|
|
656
|
+
function QRBitBuffer() {
|
|
657
|
+
this.buffer = [];
|
|
658
|
+
this.length = 0;
|
|
659
|
+
}
|
|
660
|
+
QRBitBuffer.prototype = {
|
|
661
|
+
get: function(index) {
|
|
662
|
+
var bufIndex = Math.floor(index / 8);
|
|
663
|
+
return (this.buffer[bufIndex] >>> 7 - index % 8 & 1) == 1;
|
|
664
|
+
},
|
|
665
|
+
put: function(num, length) {
|
|
666
|
+
for (var i = 0; i < length; i++) {
|
|
667
|
+
this.putBit((num >>> length - i - 1 & 1) == 1);
|
|
668
|
+
}
|
|
669
|
+
},
|
|
670
|
+
getLengthInBits: function() {
|
|
671
|
+
return this.length;
|
|
672
|
+
},
|
|
673
|
+
putBit: function(bit) {
|
|
674
|
+
var bufIndex = Math.floor(this.length / 8);
|
|
675
|
+
if (this.buffer.length <= bufIndex) {
|
|
676
|
+
this.buffer.push(0);
|
|
677
|
+
}
|
|
678
|
+
if (bit) {
|
|
679
|
+
this.buffer[bufIndex] |= 128 >>> this.length % 8;
|
|
680
|
+
}
|
|
681
|
+
this.length++;
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
module.exports = QRBitBuffer;
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/index.js
|
|
689
|
+
var require_QRCode = __commonJS({
|
|
690
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/vendor/QRCode/index.js"(exports, module) {
|
|
691
|
+
var QR8bitByte = require_QR8bitByte();
|
|
692
|
+
var QRUtil = require_QRUtil();
|
|
693
|
+
var QRPolynomial = require_QRPolynomial();
|
|
694
|
+
var QRRSBlock = require_QRRSBlock();
|
|
695
|
+
var QRBitBuffer = require_QRBitBuffer();
|
|
696
|
+
function QRCode(typeNumber, errorCorrectLevel) {
|
|
697
|
+
this.typeNumber = typeNumber;
|
|
698
|
+
this.errorCorrectLevel = errorCorrectLevel;
|
|
699
|
+
this.modules = null;
|
|
700
|
+
this.moduleCount = 0;
|
|
701
|
+
this.dataCache = null;
|
|
702
|
+
this.dataList = [];
|
|
703
|
+
}
|
|
704
|
+
QRCode.prototype = {
|
|
705
|
+
addData: function(data) {
|
|
706
|
+
var newData = new QR8bitByte(data);
|
|
707
|
+
this.dataList.push(newData);
|
|
708
|
+
this.dataCache = null;
|
|
709
|
+
},
|
|
710
|
+
isDark: function(row, col) {
|
|
711
|
+
if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) {
|
|
712
|
+
throw new Error(row + "," + col);
|
|
713
|
+
}
|
|
714
|
+
return this.modules[row][col];
|
|
715
|
+
},
|
|
716
|
+
getModuleCount: function() {
|
|
717
|
+
return this.moduleCount;
|
|
718
|
+
},
|
|
719
|
+
make: function() {
|
|
720
|
+
if (this.typeNumber < 1) {
|
|
721
|
+
var typeNumber = 1;
|
|
722
|
+
for (typeNumber = 1; typeNumber < 40; typeNumber++) {
|
|
723
|
+
var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, this.errorCorrectLevel);
|
|
724
|
+
var buffer = new QRBitBuffer();
|
|
725
|
+
var totalDataCount = 0;
|
|
726
|
+
for (var i = 0; i < rsBlocks.length; i++) {
|
|
727
|
+
totalDataCount += rsBlocks[i].dataCount;
|
|
728
|
+
}
|
|
729
|
+
for (var x = 0; x < this.dataList.length; x++) {
|
|
730
|
+
var data = this.dataList[x];
|
|
731
|
+
buffer.put(data.mode, 4);
|
|
732
|
+
buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber));
|
|
733
|
+
data.write(buffer);
|
|
734
|
+
}
|
|
735
|
+
if (buffer.getLengthInBits() <= totalDataCount * 8)
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
this.typeNumber = typeNumber;
|
|
739
|
+
}
|
|
740
|
+
this.makeImpl(false, this.getBestMaskPattern());
|
|
741
|
+
},
|
|
742
|
+
makeImpl: function(test, maskPattern) {
|
|
743
|
+
this.moduleCount = this.typeNumber * 4 + 17;
|
|
744
|
+
this.modules = new Array(this.moduleCount);
|
|
745
|
+
for (var row = 0; row < this.moduleCount; row++) {
|
|
746
|
+
this.modules[row] = new Array(this.moduleCount);
|
|
747
|
+
for (var col = 0; col < this.moduleCount; col++) {
|
|
748
|
+
this.modules[row][col] = null;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
this.setupPositionProbePattern(0, 0);
|
|
752
|
+
this.setupPositionProbePattern(this.moduleCount - 7, 0);
|
|
753
|
+
this.setupPositionProbePattern(0, this.moduleCount - 7);
|
|
754
|
+
this.setupPositionAdjustPattern();
|
|
755
|
+
this.setupTimingPattern();
|
|
756
|
+
this.setupTypeInfo(test, maskPattern);
|
|
757
|
+
if (this.typeNumber >= 7) {
|
|
758
|
+
this.setupTypeNumber(test);
|
|
759
|
+
}
|
|
760
|
+
if (this.dataCache === null) {
|
|
761
|
+
this.dataCache = QRCode.createData(this.typeNumber, this.errorCorrectLevel, this.dataList);
|
|
762
|
+
}
|
|
763
|
+
this.mapData(this.dataCache, maskPattern);
|
|
764
|
+
},
|
|
765
|
+
setupPositionProbePattern: function(row, col) {
|
|
766
|
+
for (var r = -1; r <= 7; r++) {
|
|
767
|
+
if (row + r <= -1 || this.moduleCount <= row + r) continue;
|
|
768
|
+
for (var c = -1; c <= 7; c++) {
|
|
769
|
+
if (col + c <= -1 || this.moduleCount <= col + c) continue;
|
|
770
|
+
if (0 <= r && r <= 6 && (c === 0 || c === 6) || 0 <= c && c <= 6 && (r === 0 || r === 6) || 2 <= r && r <= 4 && 2 <= c && c <= 4) {
|
|
771
|
+
this.modules[row + r][col + c] = true;
|
|
772
|
+
} else {
|
|
773
|
+
this.modules[row + r][col + c] = false;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
},
|
|
778
|
+
getBestMaskPattern: function() {
|
|
779
|
+
var minLostPoint = 0;
|
|
780
|
+
var pattern = 0;
|
|
781
|
+
for (var i = 0; i < 8; i++) {
|
|
782
|
+
this.makeImpl(true, i);
|
|
783
|
+
var lostPoint = QRUtil.getLostPoint(this);
|
|
784
|
+
if (i === 0 || minLostPoint > lostPoint) {
|
|
785
|
+
minLostPoint = lostPoint;
|
|
786
|
+
pattern = i;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
return pattern;
|
|
790
|
+
},
|
|
791
|
+
createMovieClip: function(target_mc, instance_name, depth) {
|
|
792
|
+
var qr_mc = target_mc.createEmptyMovieClip(instance_name, depth);
|
|
793
|
+
var cs = 1;
|
|
794
|
+
this.make();
|
|
795
|
+
for (var row = 0; row < this.modules.length; row++) {
|
|
796
|
+
var y = row * cs;
|
|
797
|
+
for (var col = 0; col < this.modules[row].length; col++) {
|
|
798
|
+
var x = col * cs;
|
|
799
|
+
var dark = this.modules[row][col];
|
|
800
|
+
if (dark) {
|
|
801
|
+
qr_mc.beginFill(0, 100);
|
|
802
|
+
qr_mc.moveTo(x, y);
|
|
803
|
+
qr_mc.lineTo(x + cs, y);
|
|
804
|
+
qr_mc.lineTo(x + cs, y + cs);
|
|
805
|
+
qr_mc.lineTo(x, y + cs);
|
|
806
|
+
qr_mc.endFill();
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return qr_mc;
|
|
811
|
+
},
|
|
812
|
+
setupTimingPattern: function() {
|
|
813
|
+
for (var r = 8; r < this.moduleCount - 8; r++) {
|
|
814
|
+
if (this.modules[r][6] !== null) {
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
this.modules[r][6] = r % 2 === 0;
|
|
818
|
+
}
|
|
819
|
+
for (var c = 8; c < this.moduleCount - 8; c++) {
|
|
820
|
+
if (this.modules[6][c] !== null) {
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
this.modules[6][c] = c % 2 === 0;
|
|
824
|
+
}
|
|
825
|
+
},
|
|
826
|
+
setupPositionAdjustPattern: function() {
|
|
827
|
+
var pos = QRUtil.getPatternPosition(this.typeNumber);
|
|
828
|
+
for (var i = 0; i < pos.length; i++) {
|
|
829
|
+
for (var j = 0; j < pos.length; j++) {
|
|
830
|
+
var row = pos[i];
|
|
831
|
+
var col = pos[j];
|
|
832
|
+
if (this.modules[row][col] !== null) {
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
for (var r = -2; r <= 2; r++) {
|
|
836
|
+
for (var c = -2; c <= 2; c++) {
|
|
837
|
+
if (Math.abs(r) === 2 || Math.abs(c) === 2 || r === 0 && c === 0) {
|
|
838
|
+
this.modules[row + r][col + c] = true;
|
|
839
|
+
} else {
|
|
840
|
+
this.modules[row + r][col + c] = false;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
},
|
|
847
|
+
setupTypeNumber: function(test) {
|
|
848
|
+
var bits = QRUtil.getBCHTypeNumber(this.typeNumber);
|
|
849
|
+
var mod;
|
|
850
|
+
for (var i = 0; i < 18; i++) {
|
|
851
|
+
mod = !test && (bits >> i & 1) === 1;
|
|
852
|
+
this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod;
|
|
853
|
+
}
|
|
854
|
+
for (var x = 0; x < 18; x++) {
|
|
855
|
+
mod = !test && (bits >> x & 1) === 1;
|
|
856
|
+
this.modules[x % 3 + this.moduleCount - 8 - 3][Math.floor(x / 3)] = mod;
|
|
857
|
+
}
|
|
858
|
+
},
|
|
859
|
+
setupTypeInfo: function(test, maskPattern) {
|
|
860
|
+
var data = this.errorCorrectLevel << 3 | maskPattern;
|
|
861
|
+
var bits = QRUtil.getBCHTypeInfo(data);
|
|
862
|
+
var mod;
|
|
863
|
+
for (var v = 0; v < 15; v++) {
|
|
864
|
+
mod = !test && (bits >> v & 1) === 1;
|
|
865
|
+
if (v < 6) {
|
|
866
|
+
this.modules[v][8] = mod;
|
|
867
|
+
} else if (v < 8) {
|
|
868
|
+
this.modules[v + 1][8] = mod;
|
|
869
|
+
} else {
|
|
870
|
+
this.modules[this.moduleCount - 15 + v][8] = mod;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
for (var h = 0; h < 15; h++) {
|
|
874
|
+
mod = !test && (bits >> h & 1) === 1;
|
|
875
|
+
if (h < 8) {
|
|
876
|
+
this.modules[8][this.moduleCount - h - 1] = mod;
|
|
877
|
+
} else if (h < 9) {
|
|
878
|
+
this.modules[8][15 - h - 1 + 1] = mod;
|
|
879
|
+
} else {
|
|
880
|
+
this.modules[8][15 - h - 1] = mod;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
this.modules[this.moduleCount - 8][8] = !test;
|
|
884
|
+
},
|
|
885
|
+
mapData: function(data, maskPattern) {
|
|
886
|
+
var inc = -1;
|
|
887
|
+
var row = this.moduleCount - 1;
|
|
888
|
+
var bitIndex = 7;
|
|
889
|
+
var byteIndex = 0;
|
|
890
|
+
for (var col = this.moduleCount - 1; col > 0; col -= 2) {
|
|
891
|
+
if (col === 6) col--;
|
|
892
|
+
while (true) {
|
|
893
|
+
for (var c = 0; c < 2; c++) {
|
|
894
|
+
if (this.modules[row][col - c] === null) {
|
|
895
|
+
var dark = false;
|
|
896
|
+
if (byteIndex < data.length) {
|
|
897
|
+
dark = (data[byteIndex] >>> bitIndex & 1) === 1;
|
|
898
|
+
}
|
|
899
|
+
var mask = QRUtil.getMask(maskPattern, row, col - c);
|
|
900
|
+
if (mask) {
|
|
901
|
+
dark = !dark;
|
|
902
|
+
}
|
|
903
|
+
this.modules[row][col - c] = dark;
|
|
904
|
+
bitIndex--;
|
|
905
|
+
if (bitIndex === -1) {
|
|
906
|
+
byteIndex++;
|
|
907
|
+
bitIndex = 7;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
row += inc;
|
|
912
|
+
if (row < 0 || this.moduleCount <= row) {
|
|
913
|
+
row -= inc;
|
|
914
|
+
inc = -inc;
|
|
915
|
+
break;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
QRCode.PAD0 = 236;
|
|
922
|
+
QRCode.PAD1 = 17;
|
|
923
|
+
QRCode.createData = function(typeNumber, errorCorrectLevel, dataList) {
|
|
924
|
+
var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel);
|
|
925
|
+
var buffer = new QRBitBuffer();
|
|
926
|
+
for (var i = 0; i < dataList.length; i++) {
|
|
927
|
+
var data = dataList[i];
|
|
928
|
+
buffer.put(data.mode, 4);
|
|
929
|
+
buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber));
|
|
930
|
+
data.write(buffer);
|
|
931
|
+
}
|
|
932
|
+
var totalDataCount = 0;
|
|
933
|
+
for (var x = 0; x < rsBlocks.length; x++) {
|
|
934
|
+
totalDataCount += rsBlocks[x].dataCount;
|
|
935
|
+
}
|
|
936
|
+
if (buffer.getLengthInBits() > totalDataCount * 8) {
|
|
937
|
+
throw new Error("code length overflow. (" + buffer.getLengthInBits() + ">" + totalDataCount * 8 + ")");
|
|
938
|
+
}
|
|
939
|
+
if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
|
|
940
|
+
buffer.put(0, 4);
|
|
941
|
+
}
|
|
942
|
+
while (buffer.getLengthInBits() % 8 !== 0) {
|
|
943
|
+
buffer.putBit(false);
|
|
944
|
+
}
|
|
945
|
+
while (true) {
|
|
946
|
+
if (buffer.getLengthInBits() >= totalDataCount * 8) {
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
949
|
+
buffer.put(QRCode.PAD0, 8);
|
|
950
|
+
if (buffer.getLengthInBits() >= totalDataCount * 8) {
|
|
951
|
+
break;
|
|
952
|
+
}
|
|
953
|
+
buffer.put(QRCode.PAD1, 8);
|
|
954
|
+
}
|
|
955
|
+
return QRCode.createBytes(buffer, rsBlocks);
|
|
956
|
+
};
|
|
957
|
+
QRCode.createBytes = function(buffer, rsBlocks) {
|
|
958
|
+
var offset = 0;
|
|
959
|
+
var maxDcCount = 0;
|
|
960
|
+
var maxEcCount = 0;
|
|
961
|
+
var dcdata = new Array(rsBlocks.length);
|
|
962
|
+
var ecdata = new Array(rsBlocks.length);
|
|
963
|
+
for (var r = 0; r < rsBlocks.length; r++) {
|
|
964
|
+
var dcCount = rsBlocks[r].dataCount;
|
|
965
|
+
var ecCount = rsBlocks[r].totalCount - dcCount;
|
|
966
|
+
maxDcCount = Math.max(maxDcCount, dcCount);
|
|
967
|
+
maxEcCount = Math.max(maxEcCount, ecCount);
|
|
968
|
+
dcdata[r] = new Array(dcCount);
|
|
969
|
+
for (var i = 0; i < dcdata[r].length; i++) {
|
|
970
|
+
dcdata[r][i] = 255 & buffer.buffer[i + offset];
|
|
971
|
+
}
|
|
972
|
+
offset += dcCount;
|
|
973
|
+
var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
|
|
974
|
+
var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1);
|
|
975
|
+
var modPoly = rawPoly.mod(rsPoly);
|
|
976
|
+
ecdata[r] = new Array(rsPoly.getLength() - 1);
|
|
977
|
+
for (var x = 0; x < ecdata[r].length; x++) {
|
|
978
|
+
var modIndex = x + modPoly.getLength() - ecdata[r].length;
|
|
979
|
+
ecdata[r][x] = modIndex >= 0 ? modPoly.get(modIndex) : 0;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
var totalCodeCount = 0;
|
|
983
|
+
for (var y = 0; y < rsBlocks.length; y++) {
|
|
984
|
+
totalCodeCount += rsBlocks[y].totalCount;
|
|
985
|
+
}
|
|
986
|
+
var data = new Array(totalCodeCount);
|
|
987
|
+
var index = 0;
|
|
988
|
+
for (var z = 0; z < maxDcCount; z++) {
|
|
989
|
+
for (var s = 0; s < rsBlocks.length; s++) {
|
|
990
|
+
if (z < dcdata[s].length) {
|
|
991
|
+
data[index++] = dcdata[s][z];
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
for (var xx = 0; xx < maxEcCount; xx++) {
|
|
996
|
+
for (var t = 0; t < rsBlocks.length; t++) {
|
|
997
|
+
if (xx < ecdata[t].length) {
|
|
998
|
+
data[index++] = ecdata[t][xx];
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
return data;
|
|
1003
|
+
};
|
|
1004
|
+
module.exports = QRCode;
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
// ../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/lib/main.js
|
|
1009
|
+
var require_main = __commonJS({
|
|
1010
|
+
"../../node_modules/.pnpm/qrcode-terminal@0.12.0/node_modules/qrcode-terminal/lib/main.js"(exports, module) {
|
|
1011
|
+
var QRCode = require_QRCode();
|
|
1012
|
+
var QRErrorCorrectLevel = require_QRErrorCorrectLevel();
|
|
1013
|
+
var black = "\x1B[40m \x1B[0m";
|
|
1014
|
+
var white = "\x1B[47m \x1B[0m";
|
|
1015
|
+
var toCell = function(isBlack) {
|
|
1016
|
+
return isBlack ? black : white;
|
|
1017
|
+
};
|
|
1018
|
+
var repeat = function(color) {
|
|
1019
|
+
return {
|
|
1020
|
+
times: function(count) {
|
|
1021
|
+
return new Array(count).join(color);
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
};
|
|
1025
|
+
var fill = function(length, value) {
|
|
1026
|
+
var arr = new Array(length);
|
|
1027
|
+
for (var i = 0; i < length; i++) {
|
|
1028
|
+
arr[i] = value;
|
|
1029
|
+
}
|
|
1030
|
+
return arr;
|
|
1031
|
+
};
|
|
1032
|
+
module.exports = {
|
|
1033
|
+
error: QRErrorCorrectLevel.L,
|
|
1034
|
+
generate: function(input2, opts, cb) {
|
|
1035
|
+
if (typeof opts === "function") {
|
|
1036
|
+
cb = opts;
|
|
1037
|
+
opts = {};
|
|
1038
|
+
}
|
|
1039
|
+
var qrcode2 = new QRCode(-1, this.error);
|
|
1040
|
+
qrcode2.addData(input2);
|
|
1041
|
+
qrcode2.make();
|
|
1042
|
+
var output2 = "";
|
|
1043
|
+
if (opts && opts.small) {
|
|
1044
|
+
var BLACK = true, WHITE = false;
|
|
1045
|
+
var moduleCount = qrcode2.getModuleCount();
|
|
1046
|
+
var moduleData = qrcode2.modules.slice();
|
|
1047
|
+
var oddRow = moduleCount % 2 === 1;
|
|
1048
|
+
if (oddRow) {
|
|
1049
|
+
moduleData.push(fill(moduleCount, WHITE));
|
|
1050
|
+
}
|
|
1051
|
+
var platte = {
|
|
1052
|
+
WHITE_ALL: "\u2588",
|
|
1053
|
+
WHITE_BLACK: "\u2580",
|
|
1054
|
+
BLACK_WHITE: "\u2584",
|
|
1055
|
+
BLACK_ALL: " "
|
|
1056
|
+
};
|
|
1057
|
+
var borderTop = repeat(platte.BLACK_WHITE).times(moduleCount + 3);
|
|
1058
|
+
var borderBottom = repeat(platte.WHITE_BLACK).times(moduleCount + 3);
|
|
1059
|
+
output2 += borderTop + "\n";
|
|
1060
|
+
for (var row = 0; row < moduleCount; row += 2) {
|
|
1061
|
+
output2 += platte.WHITE_ALL;
|
|
1062
|
+
for (var col = 0; col < moduleCount; col++) {
|
|
1063
|
+
if (moduleData[row][col] === WHITE && moduleData[row + 1][col] === WHITE) {
|
|
1064
|
+
output2 += platte.WHITE_ALL;
|
|
1065
|
+
} else if (moduleData[row][col] === WHITE && moduleData[row + 1][col] === BLACK) {
|
|
1066
|
+
output2 += platte.WHITE_BLACK;
|
|
1067
|
+
} else if (moduleData[row][col] === BLACK && moduleData[row + 1][col] === WHITE) {
|
|
1068
|
+
output2 += platte.BLACK_WHITE;
|
|
1069
|
+
} else {
|
|
1070
|
+
output2 += platte.BLACK_ALL;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
output2 += platte.WHITE_ALL + "\n";
|
|
1074
|
+
}
|
|
1075
|
+
if (!oddRow) {
|
|
1076
|
+
output2 += borderBottom;
|
|
1077
|
+
}
|
|
1078
|
+
} else {
|
|
1079
|
+
var border = repeat(white).times(qrcode2.getModuleCount() + 3);
|
|
1080
|
+
output2 += border + "\n";
|
|
1081
|
+
qrcode2.modules.forEach(function(row2) {
|
|
1082
|
+
output2 += white;
|
|
1083
|
+
output2 += row2.map(toCell).join("");
|
|
1084
|
+
output2 += white + "\n";
|
|
1085
|
+
});
|
|
1086
|
+
output2 += border;
|
|
1087
|
+
}
|
|
1088
|
+
if (cb) cb(output2);
|
|
1089
|
+
else console.log(output2);
|
|
1090
|
+
},
|
|
1091
|
+
setErrorLevel: function(error) {
|
|
1092
|
+
this.error = QRErrorCorrectLevel[error] || this.error;
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
2
1097
|
|
|
3
1098
|
// src/cli.ts
|
|
4
|
-
import { existsSync as
|
|
5
|
-
import { homedir as
|
|
6
|
-
import { delimiter, dirname as
|
|
1099
|
+
import { existsSync as existsSync3, readFileSync as readFileSync4 } from "node:fs";
|
|
1100
|
+
import { homedir as homedir4 } from "node:os";
|
|
1101
|
+
import { delimiter, dirname as dirname4, join as join4, resolve as resolve4 } from "node:path";
|
|
7
1102
|
import { fileURLToPath } from "node:url";
|
|
8
|
-
import { spawn, spawnSync as
|
|
1103
|
+
import { spawn, spawnSync as spawnSync3 } from "node:child_process";
|
|
9
1104
|
import { createInterface } from "node:readline/promises";
|
|
10
1105
|
import { stdin as input, stdout as output } from "node:process";
|
|
11
1106
|
|
|
@@ -100,12 +1195,12 @@ function captureConfigSnapshot(path = configPath()) {
|
|
|
100
1195
|
previous: captureFile(`${path}.previous`)
|
|
101
1196
|
};
|
|
102
1197
|
}
|
|
103
|
-
function restoreConfigSnapshot(
|
|
104
|
-
const previous = `${
|
|
105
|
-
assertNotSymlink(
|
|
1198
|
+
function restoreConfigSnapshot(snapshot2) {
|
|
1199
|
+
const previous = `${snapshot2.path}.previous`;
|
|
1200
|
+
assertNotSymlink(snapshot2.path);
|
|
106
1201
|
assertNotSymlink(previous);
|
|
107
|
-
restoreFile(previous,
|
|
108
|
-
restoreFile(
|
|
1202
|
+
restoreFile(previous, snapshot2.previous);
|
|
1203
|
+
restoreFile(snapshot2.path, snapshot2.config);
|
|
109
1204
|
}
|
|
110
1205
|
function configuredRoots(config) {
|
|
111
1206
|
return Array.isArray(config.allowedRoots) ? config.allowedRoots.filter((root) => typeof root === "string" && root !== "") : [];
|
|
@@ -129,13 +1224,13 @@ function pathExists(path) {
|
|
|
129
1224
|
throw error;
|
|
130
1225
|
}
|
|
131
1226
|
}
|
|
132
|
-
function restoreFile(path,
|
|
133
|
-
if (
|
|
1227
|
+
function restoreFile(path, snapshot2) {
|
|
1228
|
+
if (snapshot2.content === null) {
|
|
134
1229
|
rmSync(path, { force: true });
|
|
135
1230
|
return;
|
|
136
1231
|
}
|
|
137
1232
|
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
138
|
-
writeBytesAtomically(path,
|
|
1233
|
+
writeBytesAtomically(path, snapshot2.content, snapshot2.mode);
|
|
139
1234
|
}
|
|
140
1235
|
function writeBytesAtomically(path, content, mode) {
|
|
141
1236
|
const temp = createTemporaryFile(path, content, mode, "atomic");
|
|
@@ -205,6 +1300,11 @@ function assertVersion(value) {
|
|
|
205
1300
|
}
|
|
206
1301
|
return value;
|
|
207
1302
|
}
|
|
1303
|
+
function updatePackageSpec(target, currentVersion) {
|
|
1304
|
+
const selected = target ?? (currentVersion.includes("-") ? "rc" : "latest");
|
|
1305
|
+
if (selected === "latest" || selected === "rc") return `@longleash/cli@${selected}`;
|
|
1306
|
+
return `@longleash/cli@${assertVersion(selected)}`;
|
|
1307
|
+
}
|
|
208
1308
|
function packageLocation(prefix) {
|
|
209
1309
|
return join2(prefix, "node_modules", "@longleash", "cli");
|
|
210
1310
|
}
|
|
@@ -319,7 +1419,7 @@ function prepareManagedInstall(version, env = process.env) {
|
|
|
319
1419
|
previousMarker = readFileSync2(marker, "utf8");
|
|
320
1420
|
}
|
|
321
1421
|
const activeCli = join2(paths.current, "node_modules", "@longleash", "cli", "bin", "longleash.mjs");
|
|
322
|
-
writeExecutableAtomically(paths.wrapper, wrapper(activeCli));
|
|
1422
|
+
writeExecutableAtomically(paths.wrapper, wrapper(activeCli, process.execPath));
|
|
323
1423
|
wrapperWritten = true;
|
|
324
1424
|
writeFileAtomically(marker, `${JSON.stringify({ schema: 1, package: PACKAGE_NAME }, null, 2)}
|
|
325
1425
|
`, 384);
|
|
@@ -391,10 +1491,10 @@ function uninstallManagedRuntime(env = process.env) {
|
|
|
391
1491
|
rmSync2(paths.home, { recursive: true });
|
|
392
1492
|
return { removed: true, configPreserved: resolve2(env.LONGLEASH_DATA ?? join2(homedir2(), ".longleash")) };
|
|
393
1493
|
}
|
|
394
|
-
function wrapper(cli) {
|
|
1494
|
+
function wrapper(cli, node) {
|
|
395
1495
|
return `#!/bin/sh
|
|
396
1496
|
${WRAPPER_MARKER}
|
|
397
|
-
exec node ${shellQuote(cli)} "$@"
|
|
1497
|
+
exec ${shellQuote(node)} ${shellQuote(cli)} "$@"
|
|
398
1498
|
`;
|
|
399
1499
|
}
|
|
400
1500
|
function shellQuote(value) {
|
|
@@ -516,15 +1616,492 @@ function processAlive(pid) {
|
|
|
516
1616
|
}
|
|
517
1617
|
}
|
|
518
1618
|
|
|
1619
|
+
// src/service.ts
|
|
1620
|
+
import {
|
|
1621
|
+
chmodSync as chmodSync2,
|
|
1622
|
+
existsSync as existsSync2,
|
|
1623
|
+
lstatSync as lstatSync3,
|
|
1624
|
+
mkdirSync as mkdirSync3,
|
|
1625
|
+
readFileSync as readFileSync3,
|
|
1626
|
+
renameSync as renameSync3,
|
|
1627
|
+
rmSync as rmSync3,
|
|
1628
|
+
writeFileSync as writeFileSync3
|
|
1629
|
+
} from "node:fs";
|
|
1630
|
+
import { homedir as homedir3 } from "node:os";
|
|
1631
|
+
import { dirname as dirname3, join as join3, resolve as resolve3 } from "node:path";
|
|
1632
|
+
import { randomUUID as randomUUID3 } from "node:crypto";
|
|
1633
|
+
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
1634
|
+
var SERVICE_LABEL = "dev.longleash.daemon";
|
|
1635
|
+
var SYSTEMD_UNIT = "longleash.service";
|
|
1636
|
+
var MANAGED_MARKER = "Managed by @longleash/cli";
|
|
1637
|
+
function servicePaths(context = {}) {
|
|
1638
|
+
const env = context.env ?? process.env;
|
|
1639
|
+
const platform = supportedPlatform(context.platform ?? process.platform);
|
|
1640
|
+
const home = resolve3(context.home ?? homedir3());
|
|
1641
|
+
const data = dataDir(env);
|
|
1642
|
+
const logs = join3(data, "logs");
|
|
1643
|
+
const wrapper2 = installPaths(env).wrapper;
|
|
1644
|
+
if (platform === "darwin") {
|
|
1645
|
+
return {
|
|
1646
|
+
data,
|
|
1647
|
+
logs,
|
|
1648
|
+
stdout: join3(logs, "daemon.stdout.log"),
|
|
1649
|
+
stderr: join3(logs, "daemon.stderr.log"),
|
|
1650
|
+
definition: join3(home, "Library", "LaunchAgents", `${SERVICE_LABEL}.plist`),
|
|
1651
|
+
wrapper: wrapper2
|
|
1652
|
+
};
|
|
1653
|
+
}
|
|
1654
|
+
return {
|
|
1655
|
+
data,
|
|
1656
|
+
logs,
|
|
1657
|
+
stdout: "journal",
|
|
1658
|
+
stderr: "journal",
|
|
1659
|
+
definition: join3(home, ".config", "systemd", "user", SYSTEMD_UNIT),
|
|
1660
|
+
environment: join3(home, ".config", "longleash", "service.env"),
|
|
1661
|
+
wrapper: wrapper2
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
function renderLaunchAgent(paths, env = process.env) {
|
|
1665
|
+
const path = requiredEnvironment(env, "PATH", "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin");
|
|
1666
|
+
const home = resolve3(env.HOME ?? homedir3());
|
|
1667
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
1668
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
1669
|
+
<!-- ${MANAGED_MARKER} -->
|
|
1670
|
+
<plist version="1.0">
|
|
1671
|
+
<dict>
|
|
1672
|
+
<key>Label</key>
|
|
1673
|
+
<string>${xml(SERVICE_LABEL)}</string>
|
|
1674
|
+
<key>ProgramArguments</key>
|
|
1675
|
+
<array>
|
|
1676
|
+
<string>${xml(paths.wrapper)}</string>
|
|
1677
|
+
<string>run</string>
|
|
1678
|
+
</array>
|
|
1679
|
+
<key>EnvironmentVariables</key>
|
|
1680
|
+
<dict>
|
|
1681
|
+
<key>HOME</key>
|
|
1682
|
+
<string>${xml(home)}</string>
|
|
1683
|
+
<key>PATH</key>
|
|
1684
|
+
<string>${xml(path)}</string>
|
|
1685
|
+
<key>LONGLEASH_DATA</key>
|
|
1686
|
+
<string>${xml(paths.data)}</string>
|
|
1687
|
+
<key>LONGLEASH_SERVICE</key>
|
|
1688
|
+
<string>1</string>
|
|
1689
|
+
</dict>
|
|
1690
|
+
<key>WorkingDirectory</key>
|
|
1691
|
+
<string>${xml(home)}</string>
|
|
1692
|
+
<key>RunAtLoad</key>
|
|
1693
|
+
<true/>
|
|
1694
|
+
<key>KeepAlive</key>
|
|
1695
|
+
<true/>
|
|
1696
|
+
<key>ThrottleInterval</key>
|
|
1697
|
+
<integer>15</integer>
|
|
1698
|
+
<key>ProcessType</key>
|
|
1699
|
+
<string>Background</string>
|
|
1700
|
+
<key>Umask</key>
|
|
1701
|
+
<integer>63</integer>
|
|
1702
|
+
<key>StandardOutPath</key>
|
|
1703
|
+
<string>${xml(paths.stdout)}</string>
|
|
1704
|
+
<key>StandardErrorPath</key>
|
|
1705
|
+
<string>${xml(paths.stderr)}</string>
|
|
1706
|
+
</dict>
|
|
1707
|
+
</plist>
|
|
1708
|
+
`;
|
|
1709
|
+
}
|
|
1710
|
+
function renderSystemdUnit(paths, home) {
|
|
1711
|
+
if (!paths.environment) throw new Error("The Linux service environment path is missing.");
|
|
1712
|
+
return `# ${MANAGED_MARKER}
|
|
1713
|
+
[Unit]
|
|
1714
|
+
Description=LongLeash phone control plane
|
|
1715
|
+
Documentation=https://longleash.dev/docs/background-service
|
|
1716
|
+
StartLimitIntervalSec=60
|
|
1717
|
+
StartLimitBurst=5
|
|
1718
|
+
|
|
1719
|
+
[Service]
|
|
1720
|
+
Type=simple
|
|
1721
|
+
ExecStart=${systemdQuote(paths.wrapper)} run
|
|
1722
|
+
EnvironmentFile=${systemdQuote(paths.environment)}
|
|
1723
|
+
WorkingDirectory=${systemdQuote(home)}
|
|
1724
|
+
UMask=0077
|
|
1725
|
+
Restart=on-failure
|
|
1726
|
+
RestartSec=5s
|
|
1727
|
+
TimeoutStopSec=30s
|
|
1728
|
+
KillMode=mixed
|
|
1729
|
+
|
|
1730
|
+
[Install]
|
|
1731
|
+
WantedBy=default.target
|
|
1732
|
+
`;
|
|
1733
|
+
}
|
|
1734
|
+
function renderSystemdEnvironment(paths, env = process.env) {
|
|
1735
|
+
const home = resolve3(env.HOME ?? homedir3());
|
|
1736
|
+
const path = requiredEnvironment(env, "PATH", "/usr/local/bin:/usr/bin:/bin");
|
|
1737
|
+
return `# ${MANAGED_MARKER}
|
|
1738
|
+
HOME=${environmentQuote(home)}
|
|
1739
|
+
PATH=${environmentQuote(path)}
|
|
1740
|
+
LONGLEASH_DATA=${environmentQuote(paths.data)}
|
|
1741
|
+
LONGLEASH_SERVICE=1
|
|
1742
|
+
`;
|
|
1743
|
+
}
|
|
1744
|
+
function serviceState(context = {}) {
|
|
1745
|
+
const resolved = resolveContext(context);
|
|
1746
|
+
const installed = managedDefinitionExists(resolved.paths.definition);
|
|
1747
|
+
if (installed && resolved.paths.environment) assertManagedDefinition(resolved.paths.environment);
|
|
1748
|
+
if (resolved.platform === "darwin") {
|
|
1749
|
+
const loaded2 = launchdLoaded(resolved);
|
|
1750
|
+
return {
|
|
1751
|
+
platform: "darwin",
|
|
1752
|
+
installed,
|
|
1753
|
+
loaded: loaded2,
|
|
1754
|
+
active: loaded2,
|
|
1755
|
+
definition: resolved.paths.definition,
|
|
1756
|
+
logs: resolved.paths.logs,
|
|
1757
|
+
loginOnly: false
|
|
1758
|
+
};
|
|
1759
|
+
}
|
|
1760
|
+
const loaded = run(resolved, "systemctl", ["--user", "is-enabled", "--quiet", SYSTEMD_UNIT]).status === 0;
|
|
1761
|
+
const active = run(resolved, "systemctl", ["--user", "is-active", "--quiet", SYSTEMD_UNIT]).status === 0;
|
|
1762
|
+
return {
|
|
1763
|
+
platform: "linux",
|
|
1764
|
+
installed,
|
|
1765
|
+
loaded,
|
|
1766
|
+
active,
|
|
1767
|
+
definition: resolved.paths.definition,
|
|
1768
|
+
logs: "journalctl --user-unit longleash.service",
|
|
1769
|
+
loginOnly: !lingerEnabled(resolved)
|
|
1770
|
+
};
|
|
1771
|
+
}
|
|
1772
|
+
function installService(context = {}) {
|
|
1773
|
+
const resolved = resolveContext(context);
|
|
1774
|
+
assertManagedWrapper(resolved.paths.wrapper);
|
|
1775
|
+
mkdirSync3(resolved.paths.logs, { recursive: true, mode: 448 });
|
|
1776
|
+
if (resolved.platform === "darwin") installLaunchAgent(resolved);
|
|
1777
|
+
else installSystemdService(resolved);
|
|
1778
|
+
return serviceState(context);
|
|
1779
|
+
}
|
|
1780
|
+
function startService(context = {}) {
|
|
1781
|
+
const resolved = resolveContext(context);
|
|
1782
|
+
assertServiceInstallation(resolved);
|
|
1783
|
+
if (resolved.platform === "darwin") {
|
|
1784
|
+
if (launchdLoaded(resolved)) requireSuccess(resolved, "/bin/launchctl", ["kickstart", "-k", serviceTarget(resolved)]);
|
|
1785
|
+
else requireSuccess(resolved, "/bin/launchctl", ["bootstrap", launchDomain(resolved), resolved.paths.definition]);
|
|
1786
|
+
} else {
|
|
1787
|
+
requireSuccess(resolved, "systemctl", ["--user", "start", SYSTEMD_UNIT]);
|
|
1788
|
+
}
|
|
1789
|
+
return serviceState(context);
|
|
1790
|
+
}
|
|
1791
|
+
function stopService(context = {}) {
|
|
1792
|
+
const resolved = resolveContext(context);
|
|
1793
|
+
if (resolved.platform === "darwin") {
|
|
1794
|
+
const loaded = launchdLoaded(resolved);
|
|
1795
|
+
if (loaded) {
|
|
1796
|
+
assertManagedDefinition(resolved.paths.definition);
|
|
1797
|
+
requireSuccess(resolved, "/bin/launchctl", ["bootout", serviceTarget(resolved)]);
|
|
1798
|
+
}
|
|
1799
|
+
} else if (run(resolved, "systemctl", ["--user", "is-active", "--quiet", SYSTEMD_UNIT]).status === 0) {
|
|
1800
|
+
assertManagedDefinition(resolved.paths.definition);
|
|
1801
|
+
requireSuccess(resolved, "systemctl", ["--user", "stop", SYSTEMD_UNIT]);
|
|
1802
|
+
}
|
|
1803
|
+
return serviceState(context);
|
|
1804
|
+
}
|
|
1805
|
+
function restartService(context = {}) {
|
|
1806
|
+
const resolved = resolveContext(context);
|
|
1807
|
+
assertServiceInstallation(resolved);
|
|
1808
|
+
if (resolved.platform === "darwin") {
|
|
1809
|
+
if (launchdLoaded(resolved)) requireSuccess(resolved, "/bin/launchctl", ["kickstart", "-k", serviceTarget(resolved)]);
|
|
1810
|
+
else requireSuccess(resolved, "/bin/launchctl", ["bootstrap", launchDomain(resolved), resolved.paths.definition]);
|
|
1811
|
+
} else {
|
|
1812
|
+
requireSuccess(resolved, "systemctl", ["--user", "restart", SYSTEMD_UNIT]);
|
|
1813
|
+
}
|
|
1814
|
+
return serviceState(context);
|
|
1815
|
+
}
|
|
1816
|
+
function uninstallService(context = {}) {
|
|
1817
|
+
const resolved = resolveContext(context);
|
|
1818
|
+
const installed = managedDefinitionExists(resolved.paths.definition);
|
|
1819
|
+
if (resolved.platform === "darwin") {
|
|
1820
|
+
const loaded = launchdLoaded(resolved);
|
|
1821
|
+
if (loaded && !installed) {
|
|
1822
|
+
throw new Error(`Refusing to stop an unowned launchd job without a managed definition: ${resolved.paths.definition}`);
|
|
1823
|
+
}
|
|
1824
|
+
if (loaded) requireSuccess(resolved, "/bin/launchctl", ["bootout", serviceTarget(resolved)]);
|
|
1825
|
+
if (installed) rmSync3(resolved.paths.definition);
|
|
1826
|
+
} else {
|
|
1827
|
+
if (installed) {
|
|
1828
|
+
const disabled = run(resolved, "systemctl", ["--user", "disable", "--now", SYSTEMD_UNIT]);
|
|
1829
|
+
if (disabled.status !== 0 && run(resolved, "systemctl", ["--user", "is-enabled", "--quiet", SYSTEMD_UNIT]).status === 0) {
|
|
1830
|
+
throw commandError("systemctl", ["--user", "disable", "--now", SYSTEMD_UNIT], disabled);
|
|
1831
|
+
}
|
|
1832
|
+
rmSync3(resolved.paths.definition);
|
|
1833
|
+
}
|
|
1834
|
+
if (resolved.paths.environment && managedFileExists(resolved.paths.environment)) {
|
|
1835
|
+
assertManagedDefinition(resolved.paths.environment);
|
|
1836
|
+
rmSync3(resolved.paths.environment);
|
|
1837
|
+
}
|
|
1838
|
+
requireSuccess(resolved, "systemctl", ["--user", "daemon-reload"]);
|
|
1839
|
+
run(resolved, "systemctl", ["--user", "reset-failed", SYSTEMD_UNIT]);
|
|
1840
|
+
}
|
|
1841
|
+
return serviceState(context);
|
|
1842
|
+
}
|
|
1843
|
+
function showServiceLogs(follow, context = {}) {
|
|
1844
|
+
const resolved = resolveContext(context);
|
|
1845
|
+
if (resolved.platform === "darwin") {
|
|
1846
|
+
mkdirSync3(resolved.paths.logs, { recursive: true, mode: 448 });
|
|
1847
|
+
for (const path of [resolved.paths.stdout, resolved.paths.stderr]) {
|
|
1848
|
+
if (!existsSync2(path)) writeFileSync3(path, "", { mode: 384 });
|
|
1849
|
+
}
|
|
1850
|
+
return passthrough(resolved, "/usr/bin/tail", [...follow ? ["-f"] : [], "-n", "200", resolved.paths.stdout, resolved.paths.stderr]);
|
|
1851
|
+
}
|
|
1852
|
+
return passthrough(resolved, "journalctl", ["--user-unit", SYSTEMD_UNIT, "-n", "200", "--no-pager", ...follow ? ["--follow"] : []]);
|
|
1853
|
+
}
|
|
1854
|
+
function installLaunchAgent(context) {
|
|
1855
|
+
const content = renderLaunchAgent(context.paths, context.env);
|
|
1856
|
+
const previous = snapshot(context.paths.definition);
|
|
1857
|
+
const wasLoaded = launchdLoaded(context);
|
|
1858
|
+
if (wasLoaded && previous === null) {
|
|
1859
|
+
throw new Error(`Refusing to replace an unowned launchd job without a managed definition: ${context.paths.definition}`);
|
|
1860
|
+
}
|
|
1861
|
+
if (wasLoaded) requireSuccess(context, "/bin/launchctl", ["bootout", serviceTarget(context)]);
|
|
1862
|
+
try {
|
|
1863
|
+
writeManagedAtomically(context.paths.definition, content, 384, (temporary) => {
|
|
1864
|
+
requireSuccess(context, "/usr/bin/plutil", ["-lint", temporary]);
|
|
1865
|
+
});
|
|
1866
|
+
requireSuccess(context, "/bin/launchctl", ["bootstrap", launchDomain(context), context.paths.definition]);
|
|
1867
|
+
} catch (error) {
|
|
1868
|
+
restore(context.paths.definition, previous);
|
|
1869
|
+
if (previous !== null && wasLoaded) run(context, "/bin/launchctl", ["bootstrap", launchDomain(context), context.paths.definition]);
|
|
1870
|
+
throw error;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
function installSystemdService(context) {
|
|
1874
|
+
if (!context.paths.environment) throw new Error("The Linux service environment path is missing.");
|
|
1875
|
+
const previousUnit = snapshot(context.paths.definition);
|
|
1876
|
+
const previousEnvironment = snapshot(context.paths.environment);
|
|
1877
|
+
try {
|
|
1878
|
+
writeManagedAtomically(context.paths.environment, renderSystemdEnvironment(context.paths, context.env), 384);
|
|
1879
|
+
writeManagedAtomically(context.paths.definition, renderSystemdUnit(context.paths, context.home), 384);
|
|
1880
|
+
requireSuccess(context, "systemctl", ["--user", "daemon-reload"]);
|
|
1881
|
+
requireSuccess(context, "systemctl", ["--user", "enable", "--now", SYSTEMD_UNIT]);
|
|
1882
|
+
} catch (error) {
|
|
1883
|
+
restore(context.paths.environment, previousEnvironment);
|
|
1884
|
+
restore(context.paths.definition, previousUnit);
|
|
1885
|
+
run(context, "systemctl", ["--user", "daemon-reload"]);
|
|
1886
|
+
if (previousUnit !== null) run(context, "systemctl", ["--user", "enable", "--now", SYSTEMD_UNIT]);
|
|
1887
|
+
throw error;
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
function resolveContext(context) {
|
|
1891
|
+
const env = context.env ?? process.env;
|
|
1892
|
+
const platform = supportedPlatform(context.platform ?? process.platform);
|
|
1893
|
+
const home = resolve3(context.home ?? homedir3());
|
|
1894
|
+
const uid = context.uid ?? process.getuid?.();
|
|
1895
|
+
if (!Number.isSafeInteger(uid) || uid < 0) throw new Error("Could not determine the current user id for the service manager.");
|
|
1896
|
+
return {
|
|
1897
|
+
env,
|
|
1898
|
+
platform,
|
|
1899
|
+
home,
|
|
1900
|
+
uid,
|
|
1901
|
+
runner: context.runner ?? defaultRunner,
|
|
1902
|
+
paths: servicePaths({ env, platform, home })
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
function supportedPlatform(platform) {
|
|
1906
|
+
if (platform !== "darwin" && platform !== "linux") {
|
|
1907
|
+
throw new Error(`Background services are supported on macOS and systemd Linux, not ${platform}. Use \`longleash run\` instead.`);
|
|
1908
|
+
}
|
|
1909
|
+
return platform;
|
|
1910
|
+
}
|
|
1911
|
+
function defaultRunner(file, args, options = { encoding: "utf8" }) {
|
|
1912
|
+
const result = spawnSync2(file, args, { ...options, encoding: "utf8" });
|
|
1913
|
+
return {
|
|
1914
|
+
status: result.status ?? (result.error ? 1 : 0),
|
|
1915
|
+
stdout: result.stdout ?? "",
|
|
1916
|
+
stderr: result.stderr ?? (result.error?.message ?? "")
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
function run(context, file, args) {
|
|
1920
|
+
return context.runner(file, args, { encoding: "utf8", env: context.env });
|
|
1921
|
+
}
|
|
1922
|
+
function requireSuccess(context, file, args) {
|
|
1923
|
+
const result = run(context, file, args);
|
|
1924
|
+
if (result.status !== 0) throw commandError(file, args, result);
|
|
1925
|
+
return result;
|
|
1926
|
+
}
|
|
1927
|
+
function commandError(file, args, result) {
|
|
1928
|
+
const detail = (result.stderr || result.stdout).trim().replace(/\s+/g, " ").slice(0, 400);
|
|
1929
|
+
return new Error(`${file} ${args.join(" ")} failed${detail ? `: ${detail}` : "."}`);
|
|
1930
|
+
}
|
|
1931
|
+
function passthrough(context, file, args) {
|
|
1932
|
+
const result = spawnSync2(file, args, { stdio: "inherit", env: context.env });
|
|
1933
|
+
return result.status ?? 1;
|
|
1934
|
+
}
|
|
1935
|
+
function launchDomain(context) {
|
|
1936
|
+
return `gui/${context.uid}`;
|
|
1937
|
+
}
|
|
1938
|
+
function serviceTarget(context) {
|
|
1939
|
+
return `${launchDomain(context)}/${SERVICE_LABEL}`;
|
|
1940
|
+
}
|
|
1941
|
+
function launchdLoaded(context) {
|
|
1942
|
+
return run(context, "/bin/launchctl", ["print", serviceTarget(context)]).status === 0;
|
|
1943
|
+
}
|
|
1944
|
+
function lingerEnabled(context) {
|
|
1945
|
+
const result = run(context, "loginctl", ["show-user", String(context.uid), "--property=Linger", "--value"]);
|
|
1946
|
+
return result.status === 0 && result.stdout.trim().toLowerCase() === "yes";
|
|
1947
|
+
}
|
|
1948
|
+
function assertManagedWrapper(path) {
|
|
1949
|
+
if (!existsSync2(path)) throw new Error(`Managed LongLeash executable is missing: ${path}. Run \`longleash setup\` first.`);
|
|
1950
|
+
if (lstatSync3(path).isSymbolicLink()) throw new Error(`Refusing symlinked LongLeash executable: ${path}`);
|
|
1951
|
+
if (!readFileSync3(path, "utf8").includes(MANAGED_MARKER)) throw new Error(`Refusing unmanaged LongLeash executable: ${path}`);
|
|
1952
|
+
}
|
|
1953
|
+
function assertManagedDefinition(path) {
|
|
1954
|
+
if (!managedFileExists(path)) throw new Error(`LongLeash background service is not installed: ${path}`);
|
|
1955
|
+
if (!readFileSync3(path, "utf8").includes(MANAGED_MARKER)) throw new Error(`Refusing unmanaged service definition: ${path}`);
|
|
1956
|
+
}
|
|
1957
|
+
function assertServiceInstallation(context) {
|
|
1958
|
+
assertManagedDefinition(context.paths.definition);
|
|
1959
|
+
if (context.paths.environment) assertManagedDefinition(context.paths.environment);
|
|
1960
|
+
assertManagedWrapper(context.paths.wrapper);
|
|
1961
|
+
}
|
|
1962
|
+
function managedDefinitionExists(path) {
|
|
1963
|
+
if (!managedFileExists(path)) return false;
|
|
1964
|
+
assertManagedDefinition(path);
|
|
1965
|
+
return true;
|
|
1966
|
+
}
|
|
1967
|
+
function managedFileExists(path) {
|
|
1968
|
+
if (!existsSync2(path)) return false;
|
|
1969
|
+
if (lstatSync3(path).isSymbolicLink()) throw new Error(`Refusing symlinked service path: ${path}`);
|
|
1970
|
+
return true;
|
|
1971
|
+
}
|
|
1972
|
+
function snapshot(path) {
|
|
1973
|
+
if (!managedFileExists(path)) return null;
|
|
1974
|
+
assertManagedDefinition(path);
|
|
1975
|
+
return { content: readFileSync3(path, "utf8"), mode: lstatSync3(path).mode & 511 };
|
|
1976
|
+
}
|
|
1977
|
+
function restore(path, previous) {
|
|
1978
|
+
if (previous === null) rmSync3(path, { force: true });
|
|
1979
|
+
else writeManagedAtomically(path, previous.content, previous.mode);
|
|
1980
|
+
}
|
|
1981
|
+
function writeManagedAtomically(path, content, mode, validate) {
|
|
1982
|
+
if (!content.includes(MANAGED_MARKER)) throw new Error("Refusing to write an unmarked service definition.");
|
|
1983
|
+
if (managedFileExists(path)) assertManagedDefinition(path);
|
|
1984
|
+
mkdirSync3(dirname3(path), { recursive: true, mode: 448 });
|
|
1985
|
+
const temporary = `${path}.${process.pid}.${randomUUID3()}.tmp`;
|
|
1986
|
+
try {
|
|
1987
|
+
writeFileSync3(temporary, content, { encoding: "utf8", mode, flag: "wx" });
|
|
1988
|
+
chmodSync2(temporary, mode);
|
|
1989
|
+
validate?.(temporary);
|
|
1990
|
+
renameSync3(temporary, path);
|
|
1991
|
+
} catch (error) {
|
|
1992
|
+
rmSync3(temporary, { force: true });
|
|
1993
|
+
throw error;
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
function requiredEnvironment(env, name, fallback) {
|
|
1997
|
+
const value = env[name] || fallback;
|
|
1998
|
+
if (/[\0\r\n]/.test(value)) throw new Error(`Unsafe ${name} value for background service.`);
|
|
1999
|
+
return value;
|
|
2000
|
+
}
|
|
2001
|
+
function xml(value) {
|
|
2002
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
2003
|
+
}
|
|
2004
|
+
function systemdQuote(value) {
|
|
2005
|
+
if (/[\0\r\n]/.test(value)) throw new Error("Service paths may not contain control characters.");
|
|
2006
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/%/g, "%%")}"`;
|
|
2007
|
+
}
|
|
2008
|
+
function environmentQuote(value) {
|
|
2009
|
+
if (/[\0\r\n]/.test(value)) throw new Error("Service environment values may not contain control characters.");
|
|
2010
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
// src/terminal-qr.ts
|
|
2014
|
+
var import_qrcode_terminal = __toESM(require_main(), 1);
|
|
2015
|
+
function terminalQr(input2) {
|
|
2016
|
+
const source = qrModules(input2);
|
|
2017
|
+
const quiet = 4;
|
|
2018
|
+
const whiteRow = Array(source.length + quiet * 2).fill(false);
|
|
2019
|
+
const padded = [
|
|
2020
|
+
...Array.from({ length: quiet }, () => [...whiteRow]),
|
|
2021
|
+
...source.map((row) => [
|
|
2022
|
+
...Array(quiet).fill(false),
|
|
2023
|
+
...row,
|
|
2024
|
+
...Array(quiet).fill(false)
|
|
2025
|
+
]),
|
|
2026
|
+
...Array.from({ length: quiet }, () => [...whiteRow])
|
|
2027
|
+
];
|
|
2028
|
+
if (padded.length % 2 !== 0) padded.push([...whiteRow]);
|
|
2029
|
+
const output2 = [];
|
|
2030
|
+
for (let row = 0; row < padded.length; row += 2) {
|
|
2031
|
+
const top = padded[row];
|
|
2032
|
+
const bottom = padded[row + 1];
|
|
2033
|
+
if (!top || !bottom) throw new Error("QR row pairing failed");
|
|
2034
|
+
let line = "";
|
|
2035
|
+
let lastStyle = "";
|
|
2036
|
+
for (let col = 0; col < top.length; col += 1) {
|
|
2037
|
+
const rendered = cell(top[col] ?? false, bottom[col] ?? false);
|
|
2038
|
+
if (rendered.style !== lastStyle) {
|
|
2039
|
+
line += rendered.style;
|
|
2040
|
+
lastStyle = rendered.style;
|
|
2041
|
+
}
|
|
2042
|
+
line += rendered.glyph;
|
|
2043
|
+
}
|
|
2044
|
+
output2.push(`${line}\x1B[0m`);
|
|
2045
|
+
}
|
|
2046
|
+
return output2.join("\n");
|
|
2047
|
+
}
|
|
2048
|
+
function qrModules(input2) {
|
|
2049
|
+
let compact = "";
|
|
2050
|
+
import_qrcode_terminal.default.generate(input2, { small: true }, (value) => {
|
|
2051
|
+
compact = value;
|
|
2052
|
+
});
|
|
2053
|
+
const lines = compact.replace(/\n$/, "").split("\n");
|
|
2054
|
+
if (lines.length < 2) throw new Error("QR renderer returned no module rows");
|
|
2055
|
+
const moduleCount = [...lines[1] ?? ""].length - 2;
|
|
2056
|
+
if (moduleCount < 21) throw new Error("QR renderer returned an invalid module grid");
|
|
2057
|
+
const modules = [];
|
|
2058
|
+
for (const line of lines.slice(1)) {
|
|
2059
|
+
const cells = [...line].slice(1, moduleCount + 1);
|
|
2060
|
+
if (cells.length !== moduleCount) throw new Error("QR renderer returned a ragged module grid");
|
|
2061
|
+
const top = [];
|
|
2062
|
+
const bottom = [];
|
|
2063
|
+
for (const value of cells) {
|
|
2064
|
+
if (value === "\u2588") {
|
|
2065
|
+
top.push(false);
|
|
2066
|
+
bottom.push(false);
|
|
2067
|
+
} else if (value === "\u2580") {
|
|
2068
|
+
top.push(false);
|
|
2069
|
+
bottom.push(true);
|
|
2070
|
+
} else if (value === "\u2584") {
|
|
2071
|
+
top.push(true);
|
|
2072
|
+
bottom.push(false);
|
|
2073
|
+
} else if (value === " ") {
|
|
2074
|
+
top.push(true);
|
|
2075
|
+
bottom.push(true);
|
|
2076
|
+
} else throw new Error(`QR renderer returned an unknown cell ${JSON.stringify(value)}`);
|
|
2077
|
+
}
|
|
2078
|
+
modules.push(top, bottom);
|
|
2079
|
+
}
|
|
2080
|
+
return modules.slice(0, moduleCount);
|
|
2081
|
+
}
|
|
2082
|
+
function cell(top, bottom) {
|
|
2083
|
+
const black = "0;0;0";
|
|
2084
|
+
const white = "255;255;255";
|
|
2085
|
+
if (top === bottom) {
|
|
2086
|
+
const colour = top ? black : white;
|
|
2087
|
+
return { style: `\x1B[38;2;${colour};48;2;${colour}m`, glyph: "\u2588" };
|
|
2088
|
+
}
|
|
2089
|
+
return {
|
|
2090
|
+
// Black remains the continuous cell background, avoiding font seams through finder patterns.
|
|
2091
|
+
style: `\x1B[38;2;${white};48;2;${black}m`,
|
|
2092
|
+
glyph: top ? "\u2584" : "\u2580"
|
|
2093
|
+
};
|
|
2094
|
+
}
|
|
2095
|
+
|
|
519
2096
|
// src/cli.ts
|
|
520
|
-
var here =
|
|
521
|
-
var packageRoot =
|
|
522
|
-
var manifest = JSON.parse(
|
|
2097
|
+
var here = dirname4(fileURLToPath(import.meta.url));
|
|
2098
|
+
var packageRoot = resolve4(here, "..");
|
|
2099
|
+
var manifest = JSON.parse(readFileSync4(join4(packageRoot, "package.json"), "utf8"));
|
|
523
2100
|
var VERSION = manifest.version;
|
|
524
|
-
var runtime =
|
|
525
|
-
var daemonEntry =
|
|
526
|
-
var hooks =
|
|
527
|
-
var devicesEntry =
|
|
2101
|
+
var runtime = join4(packageRoot, "runtime", "daemon");
|
|
2102
|
+
var daemonEntry = join4(runtime, "bin", "longleashd.mjs");
|
|
2103
|
+
var hooks = join4(runtime, "hooks");
|
|
2104
|
+
var devicesEntry = join4(runtime, "bin", "longleash-devices.mjs");
|
|
528
2105
|
async function main() {
|
|
529
2106
|
const args = process.argv.slice(2);
|
|
530
2107
|
const command = args[0];
|
|
@@ -541,11 +2118,13 @@ async function main() {
|
|
|
541
2118
|
return 0;
|
|
542
2119
|
}
|
|
543
2120
|
if (command === "setup") return setup(parseSetup(args.slice(1)));
|
|
2121
|
+
if (command === "service") return serviceCommand(args.slice(1));
|
|
2122
|
+
if (command === "pair") return pair();
|
|
544
2123
|
if (command === "doctor") return doctor(args.includes("--json"));
|
|
545
2124
|
if (command === "hooks") return runHooks(args.slice(1));
|
|
546
2125
|
if (command === "devices") return runNode(devicesEntry, []);
|
|
547
2126
|
if (command === "revoke") return runNode(devicesEntry, ["revoke", ...args.slice(1)]);
|
|
548
|
-
if (command === "update") return update(args[1]
|
|
2127
|
+
if (command === "update") return update(args[1]);
|
|
549
2128
|
if (command === "uninstall") return uninstall();
|
|
550
2129
|
if (command === "run") return runDaemon(args.slice(1));
|
|
551
2130
|
if (command?.startsWith("-")) {
|
|
@@ -561,20 +2140,33 @@ function help() {
|
|
|
561
2140
|
|
|
562
2141
|
Usage:
|
|
563
2142
|
longleash setup configure a stable user-local installation
|
|
2143
|
+
longleash service status show manager and authenticated daemon health
|
|
2144
|
+
longleash service start start the per-user background service
|
|
2145
|
+
longleash service stop stop it without removing settings or devices
|
|
2146
|
+
longleash service restart restart it after a configuration change
|
|
2147
|
+
longleash service logs [-f] show redacted persistent service logs
|
|
2148
|
+
longleash service uninstall remove only the background service
|
|
2149
|
+
longleash pair print a fresh pairing QR from the running service
|
|
564
2150
|
longleash run [folders...] run in the foreground (terminal stays open)
|
|
565
2151
|
longleash [folders...] shorthand for run
|
|
566
2152
|
longleash doctor [--json] report package, config, and daemon health
|
|
567
2153
|
longleash hooks [--remove] install or remove supported provider hooks
|
|
568
2154
|
longleash devices list paired phones
|
|
569
2155
|
longleash revoke <id|--all> revoke paired phone access
|
|
570
|
-
longleash update [version] atomically
|
|
2156
|
+
longleash update [version] atomically update this channel or install an exact version
|
|
571
2157
|
longleash uninstall remove runtime and hooks; preserve user data
|
|
572
|
-
longleash where print the active npm package directory
|
|
573
|
-
|
|
574
|
-
Setup never starts the daemon. Background service commands arrive in Workstream C.`);
|
|
2158
|
+
longleash where print the active npm package directory`);
|
|
575
2159
|
}
|
|
576
2160
|
function parseSetup(args) {
|
|
577
|
-
const options = {
|
|
2161
|
+
const options = {
|
|
2162
|
+
roots: [],
|
|
2163
|
+
relay: "",
|
|
2164
|
+
yes: false,
|
|
2165
|
+
reuse: false,
|
|
2166
|
+
configureOnly: false,
|
|
2167
|
+
skipHooks: false,
|
|
2168
|
+
service: "default"
|
|
2169
|
+
};
|
|
578
2170
|
for (let i = 0; i < args.length; i += 1) {
|
|
579
2171
|
const arg = args[i];
|
|
580
2172
|
if (arg === "--root") {
|
|
@@ -589,6 +2181,8 @@ function parseSetup(args) {
|
|
|
589
2181
|
else if (arg === "--reuse-config") options.reuse = true;
|
|
590
2182
|
else if (arg === "--configure-only") options.configureOnly = true;
|
|
591
2183
|
else if (arg === "--skip-hooks") options.skipHooks = true;
|
|
2184
|
+
else if (arg === "--service") options.service = "install";
|
|
2185
|
+
else if (arg === "--no-service") options.service = "skip";
|
|
592
2186
|
else throw new Error(`Unknown setup option: ${arg}`);
|
|
593
2187
|
}
|
|
594
2188
|
return options;
|
|
@@ -608,6 +2202,11 @@ async function setup(options) {
|
|
|
608
2202
|
options.relay = typeof existing.relayUrl === "string" ? existing.relayUrl : "off";
|
|
609
2203
|
options.yes = true;
|
|
610
2204
|
}
|
|
2205
|
+
let existingService = false;
|
|
2206
|
+
try {
|
|
2207
|
+
existingService = serviceState().installed;
|
|
2208
|
+
} catch {
|
|
2209
|
+
}
|
|
611
2210
|
if (options.roots.length === 0 && options.yes) {
|
|
612
2211
|
throw new Error("Non-interactive setup requires at least one explicit --root, or --reuse-config.");
|
|
613
2212
|
}
|
|
@@ -624,12 +2223,14 @@ async function setup(options) {
|
|
|
624
2223
|
console.log("\nConnectivity: hosted works away from home; LAN-only has no internet relay.");
|
|
625
2224
|
const relayAnswer = await prompt.question("Relay [hosted / lan / wss://your-relay/ws] (hosted): ");
|
|
626
2225
|
options.relay = relayAnswer.trim() || "hosted";
|
|
2226
|
+
const serviceAnswer = await prompt.question("Keep LongLeash available after this terminal closes? [Y/n] ");
|
|
2227
|
+
options.service = /^n(?:o)?$/i.test(serviceAnswer.trim()) ? "skip" : "install";
|
|
627
2228
|
const roots2 = normalizeRoots(options.roots);
|
|
628
2229
|
const relay2 = normalizeRelay(options.relay);
|
|
629
2230
|
console.log("\nReview before anything changes:");
|
|
630
2231
|
for (const root of roots2) console.log(` allowed root ${root}`);
|
|
631
2232
|
console.log(` connectivity ${relay2 ?? "LAN-only (no relay)"}`);
|
|
632
|
-
console.log(
|
|
2233
|
+
console.log(` service ${options.service === "install" ? "per-user background service at login" : "foreground only"}`);
|
|
633
2234
|
const accepted = await prompt.question("\nApply this setup? [y/N] ");
|
|
634
2235
|
if (!/^y(?:es)?$/i.test(accepted.trim())) {
|
|
635
2236
|
console.log("Nothing changed.");
|
|
@@ -653,7 +2254,7 @@ async function setup(options) {
|
|
|
653
2254
|
relay ?? "off",
|
|
654
2255
|
"--skip-hooks"
|
|
655
2256
|
];
|
|
656
|
-
const configured =
|
|
2257
|
+
const configured = spawnSync3(process.execPath, [prepared.cli, ...forwarded], {
|
|
657
2258
|
stdio: "inherit",
|
|
658
2259
|
env: { ...process.env, LONGLEASH_INTERNAL_CONFIGURE: VERSION }
|
|
659
2260
|
});
|
|
@@ -672,7 +2273,7 @@ async function setup(options) {
|
|
|
672
2273
|
console.log(`
|
|
673
2274
|
Activated @longleash/cli ${VERSION}.`);
|
|
674
2275
|
if (!options.skipHooks) {
|
|
675
|
-
const hooked =
|
|
2276
|
+
const hooked = spawnSync3(process.execPath, [prepared.cli, "hooks"], { stdio: "inherit", env: process.env });
|
|
676
2277
|
if (hooked.error || hooked.status !== 0) {
|
|
677
2278
|
console.error("Provider hook setup needs attention. The runtime is installed; run `longleash hooks` after fixing the message above.");
|
|
678
2279
|
}
|
|
@@ -687,6 +2288,22 @@ The executable is installed at ${paths.wrapper}, but ${paths.bin} is not on PATH
|
|
|
687
2288
|
console.log(` export PATH=${quoteForShell(paths.bin)}:"$PATH"`);
|
|
688
2289
|
console.log(`You can use ${paths.wrapper} immediately.`);
|
|
689
2290
|
}
|
|
2291
|
+
const manageService = options.service === "install" || options.service === "default" && (options.reuse ? existingService : true);
|
|
2292
|
+
if (manageService) {
|
|
2293
|
+
try {
|
|
2294
|
+
const state = installService();
|
|
2295
|
+
const healthy = await waitForDaemon(15e3);
|
|
2296
|
+
if (!healthy) {
|
|
2297
|
+
throw new Error("The service manager started LongLeash, but its authenticated health check did not become ready. Run `longleash service logs`.");
|
|
2298
|
+
}
|
|
2299
|
+
console.log(`Background service installed and running (${state.platform}).`);
|
|
2300
|
+
console.log("Run `longleash pair` for a fresh QR; pairing secrets are not stored in service logs.");
|
|
2301
|
+
} catch (error) {
|
|
2302
|
+
throw new Error(`The verified runtime is installed, but the background service could not be activated: ${message(error)} You can use \`longleash run\` while diagnosing it.`);
|
|
2303
|
+
}
|
|
2304
|
+
} else {
|
|
2305
|
+
console.log("Background service was not installed. Run `longleash run` for foreground use or `longleash service install` later.");
|
|
2306
|
+
}
|
|
690
2307
|
return 0;
|
|
691
2308
|
}
|
|
692
2309
|
const next = { ...existing, cliSchemaVersion: 1, allowedRoots: roots };
|
|
@@ -694,17 +2311,98 @@ The executable is installed at ${paths.wrapper}, but ${paths.bin} is not on PATH
|
|
|
694
2311
|
else next.relayUrl = relay;
|
|
695
2312
|
saveConfig(next);
|
|
696
2313
|
console.log(`Saved configuration to ${configPath()}`);
|
|
697
|
-
console.log("
|
|
2314
|
+
console.log("Configuration saved. The outer setup transaction decides whether to start the service.");
|
|
2315
|
+
return 0;
|
|
2316
|
+
}
|
|
2317
|
+
async function serviceCommand(args) {
|
|
2318
|
+
const command = args[0] ?? "status";
|
|
2319
|
+
if (command === "install") {
|
|
2320
|
+
const state = installService();
|
|
2321
|
+
if (!await waitForDaemon(15e3)) throw new Error("The service was installed but did not become healthy. Run `longleash service logs`.");
|
|
2322
|
+
printServiceState(state, true);
|
|
2323
|
+
return 0;
|
|
2324
|
+
}
|
|
2325
|
+
if (command === "start") {
|
|
2326
|
+
const state = startService();
|
|
2327
|
+
if (!await waitForDaemon(15e3)) throw new Error("The service manager started the job but the daemon did not become healthy. Run `longleash service logs`.");
|
|
2328
|
+
printServiceState(state, true);
|
|
2329
|
+
return 0;
|
|
2330
|
+
}
|
|
2331
|
+
if (command === "stop") {
|
|
2332
|
+
const state = stopService();
|
|
2333
|
+
printServiceState(state, false);
|
|
2334
|
+
return 0;
|
|
2335
|
+
}
|
|
2336
|
+
if (command === "restart") {
|
|
2337
|
+
const state = restartService();
|
|
2338
|
+
if (!await waitForDaemon(15e3)) throw new Error("The service restarted but the daemon did not become healthy. Run `longleash service logs`.");
|
|
2339
|
+
printServiceState(state, true);
|
|
2340
|
+
return 0;
|
|
2341
|
+
}
|
|
2342
|
+
if (command === "uninstall") {
|
|
2343
|
+
uninstallService();
|
|
2344
|
+
console.log(`Removed the per-user background service. Settings, devices, audit data, and runtime remain in ${configPath()}.`);
|
|
2345
|
+
return 0;
|
|
2346
|
+
}
|
|
2347
|
+
if (command === "logs") return showServiceLogs(args.includes("--follow") || args.includes("-f"));
|
|
2348
|
+
if (command === "status") {
|
|
2349
|
+
const state = serviceState();
|
|
2350
|
+
const healthy = await daemonHealth();
|
|
2351
|
+
if (args.includes("--json")) console.log(JSON.stringify({ ...state, healthy }, null, 2));
|
|
2352
|
+
else printServiceState(state, healthy);
|
|
2353
|
+
return state.installed && healthy ? 0 : 1;
|
|
2354
|
+
}
|
|
2355
|
+
throw new Error(`Unknown service command: ${command}`);
|
|
2356
|
+
}
|
|
2357
|
+
function printServiceState(state, healthy) {
|
|
2358
|
+
console.log("");
|
|
2359
|
+
console.log(` service ${state.installed ? "installed" : "not installed"} (${state.platform})`);
|
|
2360
|
+
console.log(` manager ${state.loaded ? "loaded" : "not loaded"}${state.active ? " \xB7 active" : ""}`);
|
|
2361
|
+
console.log(` daemon ${healthy ? "authenticated health check passed" : "not reachable"}`);
|
|
2362
|
+
console.log(` definition ${state.definition}`);
|
|
2363
|
+
console.log(` logs ${state.logs}`);
|
|
2364
|
+
if (state.platform === "linux" && state.loginOnly) {
|
|
2365
|
+
console.log(" login scope stops after logout; optional lingering is not enabled by LongLeash");
|
|
2366
|
+
}
|
|
2367
|
+
console.log("");
|
|
2368
|
+
}
|
|
2369
|
+
async function pair() {
|
|
2370
|
+
const response = await localDaemonRequest("/local/pairing", { method: "POST" });
|
|
2371
|
+
if (!response.ok) throw new Error(`The running daemon could not create a pairing challenge (${response.status}).`);
|
|
2372
|
+
const body = await response.json();
|
|
2373
|
+
if (typeof body.url !== "string" || !/^https?:\/\//.test(body.url)) throw new Error("The daemon returned an invalid pairing challenge.");
|
|
2374
|
+
console.log("\nScan this with your phone. The link is single-use and expires:\n");
|
|
2375
|
+
console.log(terminalQr(body.url));
|
|
2376
|
+
console.log(`
|
|
2377
|
+
${body.url}
|
|
2378
|
+
`);
|
|
698
2379
|
return 0;
|
|
699
2380
|
}
|
|
700
2381
|
async function runDaemon(explicitRoots) {
|
|
701
|
-
if (!
|
|
2382
|
+
if (!existsSync3(daemonEntry)) throw new Error("The npm package is incomplete: daemon runtime is missing.");
|
|
702
2383
|
const config = loadConfig();
|
|
703
2384
|
const roots = normalizeRoots(explicitRoots.length > 0 ? explicitRoots : configuredRoots(config));
|
|
704
2385
|
const child = spawn(process.execPath, [daemonEntry, ...roots], { stdio: "inherit", env: process.env });
|
|
705
2386
|
return await new Promise((resolveExit, reject) => {
|
|
706
|
-
|
|
707
|
-
|
|
2387
|
+
const forward = (signal) => {
|
|
2388
|
+
if (child.exitCode === null && child.signalCode === null) child.kill(signal);
|
|
2389
|
+
};
|
|
2390
|
+
const interrupt = () => forward("SIGINT");
|
|
2391
|
+
const terminate = () => forward("SIGTERM");
|
|
2392
|
+
const cleanup = () => {
|
|
2393
|
+
process.off("SIGINT", interrupt);
|
|
2394
|
+
process.off("SIGTERM", terminate);
|
|
2395
|
+
};
|
|
2396
|
+
process.on("SIGINT", interrupt);
|
|
2397
|
+
process.on("SIGTERM", terminate);
|
|
2398
|
+
child.once("error", (error) => {
|
|
2399
|
+
cleanup();
|
|
2400
|
+
reject(error);
|
|
2401
|
+
});
|
|
2402
|
+
child.once("exit", (code, signal) => {
|
|
2403
|
+
cleanup();
|
|
2404
|
+
resolveExit(code ?? (signal ? 1 : 0));
|
|
2405
|
+
});
|
|
708
2406
|
});
|
|
709
2407
|
}
|
|
710
2408
|
async function doctor(json) {
|
|
@@ -715,10 +2413,10 @@ async function doctor(json) {
|
|
|
715
2413
|
} catch (error) {
|
|
716
2414
|
configError = message(error);
|
|
717
2415
|
}
|
|
718
|
-
const endpointPath =
|
|
2416
|
+
const endpointPath = join4(process.env.LONGLEASH_DATA ?? join4(homedir4(), ".longleash"), "hook-endpoint.json");
|
|
719
2417
|
let daemon = { reachable: false };
|
|
720
2418
|
try {
|
|
721
|
-
const endpoint = JSON.parse(
|
|
2419
|
+
const endpoint = JSON.parse(readFileSync4(endpointPath, "utf8"));
|
|
722
2420
|
if (endpoint.url && endpoint.secret) {
|
|
723
2421
|
const response = await fetch(endpoint.url.replace(/\/hook$/, "/health"), {
|
|
724
2422
|
headers: { "x-longleash-hook": endpoint.secret },
|
|
@@ -759,12 +2457,12 @@ LongLeash ${VERSION}`);
|
|
|
759
2457
|
async function runHooks(args) {
|
|
760
2458
|
const remove = args.includes("--remove");
|
|
761
2459
|
let failed = false;
|
|
762
|
-
if (commandAvailable("claude") || remove) failed = await runNode(
|
|
763
|
-
if (commandAvailable("codex") || remove) failed = await runNode(
|
|
2460
|
+
if (commandAvailable("claude") || remove) failed = await runNode(join4(hooks, "install-hooks.mjs"), remove ? ["--remove"] : []) !== 0 || failed;
|
|
2461
|
+
if (commandAvailable("codex") || remove) failed = await runNode(join4(hooks, "install-codex-hooks.mjs"), remove ? ["--remove"] : []) !== 0 || failed;
|
|
764
2462
|
return failed ? 1 : 0;
|
|
765
2463
|
}
|
|
766
2464
|
async function update(target) {
|
|
767
|
-
const spec = target
|
|
2465
|
+
const spec = updatePackageSpec(target, VERSION);
|
|
768
2466
|
const child = spawn("npm", [
|
|
769
2467
|
"exec",
|
|
770
2468
|
"--yes",
|
|
@@ -785,6 +2483,11 @@ async function update(target) {
|
|
|
785
2483
|
});
|
|
786
2484
|
}
|
|
787
2485
|
async function uninstall() {
|
|
2486
|
+
try {
|
|
2487
|
+
if (serviceState().installed) uninstallService();
|
|
2488
|
+
} catch (error) {
|
|
2489
|
+
throw new Error(`The background service could not be removed safely, so the runtime was preserved: ${message(error)}`);
|
|
2490
|
+
}
|
|
788
2491
|
const hooksRemoved = await runHooks(["--remove"]);
|
|
789
2492
|
if (hooksRemoved !== 0) throw new Error("Provider hooks could not be removed safely. The managed runtime was preserved.");
|
|
790
2493
|
const result = uninstallManagedRuntime();
|
|
@@ -792,11 +2495,56 @@ async function uninstall() {
|
|
|
792
2495
|
console.log(`Preserved settings, paired devices, and audit data in ${result.configPreserved}.`);
|
|
793
2496
|
return 0;
|
|
794
2497
|
}
|
|
2498
|
+
async function daemonHealth() {
|
|
2499
|
+
try {
|
|
2500
|
+
const response = await localDaemonRequest("/health", { method: "GET" }, 1500);
|
|
2501
|
+
if (!response.ok) return false;
|
|
2502
|
+
const body = await response.json();
|
|
2503
|
+
return body.name === "longleash";
|
|
2504
|
+
} catch {
|
|
2505
|
+
return false;
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
async function waitForDaemon(timeoutMs) {
|
|
2509
|
+
const deadline = Date.now() + timeoutMs;
|
|
2510
|
+
while (Date.now() < deadline) {
|
|
2511
|
+
if (await daemonHealth()) return true;
|
|
2512
|
+
await new Promise((resolveWait) => setTimeout(resolveWait, 200));
|
|
2513
|
+
}
|
|
2514
|
+
return false;
|
|
2515
|
+
}
|
|
2516
|
+
async function localDaemonRequest(path, init, timeoutMs = 5e3) {
|
|
2517
|
+
const endpointPath = join4(process.env.LONGLEASH_DATA ?? join4(homedir4(), ".longleash"), "hook-endpoint.json");
|
|
2518
|
+
let endpoint;
|
|
2519
|
+
try {
|
|
2520
|
+
endpoint = JSON.parse(readFileSync4(endpointPath, "utf8"));
|
|
2521
|
+
} catch {
|
|
2522
|
+
throw new Error("LongLeash is not reachable. Start it with `longleash service start` or `longleash run`.");
|
|
2523
|
+
}
|
|
2524
|
+
if (typeof endpoint.url !== "string" || typeof endpoint.secret !== "string" || endpoint.secret === "") {
|
|
2525
|
+
throw new Error(`Invalid local daemon endpoint: ${endpointPath}`);
|
|
2526
|
+
}
|
|
2527
|
+
const url = new URL(endpoint.url);
|
|
2528
|
+
if (url.protocol !== "http:" || url.pathname !== "/hook" || !localAddress(url.hostname)) {
|
|
2529
|
+
throw new Error(`Refusing a non-local daemon endpoint in ${endpointPath}`);
|
|
2530
|
+
}
|
|
2531
|
+
return fetch(`${url.origin}${path}`, {
|
|
2532
|
+
...init,
|
|
2533
|
+
headers: { ...init.headers, "x-longleash-hook": endpoint.secret },
|
|
2534
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
2535
|
+
});
|
|
2536
|
+
}
|
|
2537
|
+
function localAddress(hostname) {
|
|
2538
|
+
if (hostname === "localhost" || hostname === "::1") return true;
|
|
2539
|
+
if (/^127\./.test(hostname) || /^10\./.test(hostname) || /^192\.168\./.test(hostname)) return true;
|
|
2540
|
+
const match = hostname.match(/^172\.(\d+)\./);
|
|
2541
|
+
return match !== null && Number(match[1]) >= 16 && Number(match[1]) <= 31;
|
|
2542
|
+
}
|
|
795
2543
|
function commandAvailable(command) {
|
|
796
|
-
return
|
|
2544
|
+
return spawnSync3(command, ["--version"], { stdio: "ignore", timeout: 3e3 }).status === 0;
|
|
797
2545
|
}
|
|
798
2546
|
function pathDirectories() {
|
|
799
|
-
return (process.env.PATH ?? "").split(delimiter).filter(Boolean).map((path) =>
|
|
2547
|
+
return (process.env.PATH ?? "").split(delimiter).filter(Boolean).map((path) => resolve4(path));
|
|
800
2548
|
}
|
|
801
2549
|
function quoteForShell(value) {
|
|
802
2550
|
return `'${value.replace(/'/g, `'"'"'`)}'`;
|