@swipt/cli 0.1.0-nightly.78 → 0.1.0-nightly.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/availability.d.ts +5 -0
  2. package/dist/availability.js +349 -0
  3. package/dist/availability.js.map +1 -0
  4. package/dist/client.d.ts +25 -0
  5. package/dist/client.js +248 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/connection.d.ts +24 -0
  8. package/dist/connection.js +293 -0
  9. package/dist/connection.js.map +1 -0
  10. package/dist/coredevice.d.ts +22 -0
  11. package/dist/coredevice.js +329 -0
  12. package/dist/coredevice.js.map +1 -0
  13. package/dist/device.d.ts +130 -0
  14. package/dist/device.js +539 -0
  15. package/dist/device.js.map +1 -0
  16. package/dist/i18n.d.ts +2 -0
  17. package/dist/i18n.js +57 -0
  18. package/dist/i18n.js.map +1 -0
  19. package/dist/ignore.d.ts +8 -0
  20. package/dist/ignore.js +112 -0
  21. package/dist/ignore.js.map +1 -0
  22. package/dist/metro.d.ts +10 -0
  23. package/dist/metro.js +349 -0
  24. package/dist/metro.js.map +1 -0
  25. package/dist/output.d.ts +20 -0
  26. package/dist/output.js +116 -0
  27. package/dist/output.js.map +1 -0
  28. package/dist/project.d.ts +18 -0
  29. package/dist/project.js +696 -0
  30. package/dist/project.js.map +1 -0
  31. package/dist/proto/common_pb.js +14 -0
  32. package/dist/proto/common_pb.js.map +1 -0
  33. package/dist/proto/logs_pb.js +15 -0
  34. package/dist/proto/logs_pb.js.map +1 -0
  35. package/dist/proto/repl_pb.js +15 -0
  36. package/dist/proto/repl_pb.js.map +1 -0
  37. package/dist/proto/swiplets_pb.js +20 -0
  38. package/dist/proto/swiplets_pb.js.map +1 -0
  39. package/dist/proto/system_pb.js +15 -0
  40. package/dist/proto/system_pb.js.map +1 -0
  41. package/dist/sideload.d.ts +17 -0
  42. package/dist/sideload.js +1098 -0
  43. package/dist/sideload.js.map +1 -0
  44. package/dist/swipt.d.ts +1 -0
  45. package/dist/swipt.js +631 -0
  46. package/dist/swipt.js.map +1 -0
  47. package/dist/version.d.ts +2 -0
  48. package/dist/version.js +15 -0
  49. package/dist/version.js.map +1 -0
  50. package/package.json +21 -7
  51. package/availability.mjs +0 -642
  52. package/client.mjs +0 -375
  53. package/connection.mjs +0 -450
  54. package/coredevice.mjs +0 -464
  55. package/device.mjs +0 -837
  56. package/i18n.mjs +0 -71
  57. package/ignore.mjs +0 -149
  58. package/metro.mjs +0 -432
  59. package/output.mjs +0 -177
  60. package/project.mjs +0 -1013
  61. package/proto/common_pb.js +0 -98
  62. package/proto/logs_pb.js +0 -34
  63. package/proto/repl_pb.js +0 -55
  64. package/proto/swiplets_pb.js +0 -179
  65. package/proto/system_pb.js +0 -27
  66. package/sideload.mjs +0 -1696
  67. package/swipt.mjs +0 -726
  68. package/version.mjs +0 -16
package/coredevice.mjs DELETED
@@ -1,464 +0,0 @@
1
- import { execFile } from "node:child_process";
2
- import { randomUUID } from "node:crypto";
3
- import fs from "node:fs";
4
- import { createRequire } from "node:module";
5
- import net from "node:net";
6
- import os from "node:os";
7
- import path from "node:path";
8
- import { promisify } from "node:util";
9
-
10
- import { label, sameName } from "./connection.mjs";
11
- import { tr } from "./i18n.mjs";
12
- import { choose, interactive, step } from "./output.mjs";
13
-
14
- const exec = promisify(execFile);
15
-
16
- const xcodeMissing = tr(
17
- "Xcode is missing; install it from the App Store, open it once, and select it with xcode-select",
18
- );
19
-
20
- export async function devicectl(args, { parse = false } = {}) {
21
- const output = parse
22
- ? path.join(os.tmpdir(), `devicectl-${randomUUID()}.json`)
23
- : undefined;
24
- try {
25
- await exec(
26
- "xcrun",
27
- ["devicectl", ...args, ...(parse ? ["--json-output", output] : [])],
28
- { maxBuffer: Infinity },
29
- );
30
- } catch (error) {
31
- if (error.code === "ENOENT") {
32
- throw new Error(xcodeMissing);
33
- }
34
- if (typeof error.code !== "number") {
35
- throw error;
36
- }
37
- const text = `${error.stdout}${error.stderr}`;
38
- if (/^(xcrun|xcode-select): /m.test(text)) {
39
- throw new Error(`${xcodeMissing} (${text.trim()})`);
40
- }
41
- if (text.includes("4016")) {
42
- throw new Error(
43
- tr(
44
- "CoreDevice cannot reach the device while its loopback VPN is on; turn off Swipt's VPN on the device, then run this again",
45
- ),
46
- );
47
- }
48
- throw new Error(text.trim() || `devicectl exited with ${error.code}`);
49
- }
50
- if (!parse) {
51
- return undefined;
52
- }
53
- const parsed = JSON.parse(fs.readFileSync(output, "utf8"));
54
- fs.rmSync(output);
55
- return parsed;
56
- }
57
-
58
- export const serverHint = tr(
59
- 'swipt-server is missing; install it with "npm install -g @swipt/cli --include=optional"',
60
- );
61
-
62
- export function serverPath() {
63
- const built = path.join(
64
- path.dirname(import.meta.dirname),
65
- "build/swipt-server",
66
- );
67
- if (fs.existsSync(built)) {
68
- return built;
69
- }
70
- let packaged;
71
- try {
72
- packaged = path.join(
73
- path.dirname(
74
- createRequire(import.meta.url).resolve(
75
- `@swipt/server-${process.platform}-${process.arch}/package.json`,
76
- ),
77
- ),
78
- "swipt-server",
79
- );
80
- } catch (error) {
81
- if (error.code !== "MODULE_NOT_FOUND") {
82
- throw error;
83
- }
84
- return "swipt-server";
85
- }
86
- return fs.existsSync(packaged) ? packaged : "swipt-server";
87
- }
88
-
89
- async function guest(device, args) {
90
- try {
91
- await exec(serverPath(), [
92
- "guest",
93
- "-d",
94
- device.udid,
95
- "--socket",
96
- device.socket,
97
- ...args,
98
- ]);
99
- } catch (error) {
100
- if (error.code === "ENOENT") {
101
- throw new Error(serverHint);
102
- }
103
- throw new Error(
104
- `${error.stderr ?? ""}`.replace(/^Error: /, "").trim() || error.message,
105
- );
106
- }
107
- }
108
-
109
- function callGuest(device, method, params = {}) {
110
- return new Promise((resolve, reject) => {
111
- const socket = net.createConnection(device.socket, () =>
112
- socket.write(`${JSON.stringify({ t: "rpc", method, params })}\n`),
113
- );
114
- const chunks = [];
115
- socket.on("data", (chunk) => {
116
- chunks.push(chunk);
117
- if (chunk.includes(10)) {
118
- socket.destroy();
119
- try {
120
- const reply = JSON.parse(Buffer.concat(chunks));
121
- if (reply.ok) {
122
- resolve(reply.result);
123
- } else {
124
- reject(new Error(`${method}: ${JSON.stringify(reply.error)}`));
125
- }
126
- } catch (error) {
127
- reject(error);
128
- }
129
- }
130
- });
131
- socket.on("error", reject);
132
- socket.on("close", () =>
133
- reject(new Error(`${method}: connection closed before reply`)),
134
- );
135
- });
136
- }
137
-
138
- const launchpadSocket = path.join(
139
- os.homedir(),
140
- "Library/Application Support/vphone-launchpad/control.sock",
141
- );
142
-
143
- async function guests() {
144
- if (!fs.existsSync(launchpadSocket)) {
145
- return [];
146
- }
147
- let stdout;
148
- try {
149
- ({ stdout } = await exec("vphone-launchpad-cli", ["vm", "list"]));
150
- } catch (error) {
151
- if (error.code === "ENOENT") {
152
- return [];
153
- }
154
- throw error;
155
- }
156
- return JSON.parse(stdout)
157
- .filter((machine) => machine.state === "running" && machine.udid)
158
- .map((machine) => ({
159
- name: machine.name,
160
- udid: machine.udid,
161
- socket: machine.controlSocket,
162
- virtual: true,
163
- }));
164
- }
165
-
166
- // vphone guests are built with chip ID 0xFE01, which their UDID starts with
167
- // (vphone-cli 8c3630c7, Research/Guest/xcode_install_signature_gate.md).
168
- const guestUDID = /^0000FE01-/;
169
-
170
- const reachability = tr(
171
- "unlock the device and connect it with USB or the same network as this Mac",
172
- );
173
-
174
- const deviceLine = (device) =>
175
- ` ${device.name} ${device.udid}${device.offline ? ` ${tr("(offline)")}` : ""}`;
176
-
177
- async function findDevice(context, { required = true, filter }) {
178
- const given = context.options.device;
179
- const [listed, virtual] = await step(
180
- tr("looking for devices"),
181
- () =>
182
- Promise.all([devicectl(["list", "devices"], { parse: true }), guests()]),
183
- { transient: true },
184
- );
185
- const paired = [
186
- ...listed.result.devices
187
- .filter(
188
- (device) =>
189
- device.hardwareProperties.platform === "iOS" &&
190
- ["physical", undefined].includes(device.hardwareProperties.reality) &&
191
- !guestUDID.test(device.hardwareProperties.udid),
192
- )
193
- .map((device) => ({
194
- name: device.deviceProperties.name,
195
- udid: device.hardwareProperties.udid,
196
- offline: device.connectionProperties.tunnelState === "unavailable",
197
- })),
198
- ...virtual,
199
- ];
200
- const named = (device) =>
201
- sameName(device.udid, given) || sameName(label(device.name), label(given));
202
- const devices = paired.filter((device) => !device.offline);
203
- const matches = given ? devices.filter(named) : devices;
204
- if ((given || !required) && matches.length === 1) {
205
- return matches[0];
206
- }
207
- if (paired.length === 0) {
208
- throw new Error(
209
- tr(
210
- "no iOS device is paired with this Mac; connect one with USB, unlock it, and trust this Mac",
211
- ),
212
- );
213
- }
214
- const offline = given && paired.find(named);
215
- if (matches.length === 0 && offline) {
216
- throw new Error(
217
- tr("{device} is offline; {hint}", {
218
- device: offline.name,
219
- hint: reachability,
220
- }),
221
- );
222
- }
223
- if (devices.length === 0) {
224
- throw new Error(
225
- `${tr("every paired iOS device is offline; {hint}:", { hint: reachability })}\n${paired.map(deviceLine).join("\n")}`,
226
- );
227
- }
228
- const reason = !given
229
- ? tr("choose a device with -d NAME|UDID")
230
- : matches.length === 0
231
- ? tr("no iOS device is named {name}", { name: given })
232
- : tr("more than one iOS device is named {name}; choose one by UDID", {
233
- name: given,
234
- });
235
- const failure = new Error(
236
- `${tr("{reason}; paired iOS devices:", { reason })}\n${paired.map(deviceLine).join("\n")}`,
237
- );
238
- if (matches.length === 0) {
239
- throw failure;
240
- }
241
- const choices =
242
- filter !== undefined && interactive(context)
243
- ? await filter(matches)
244
- : matches;
245
- return choose(
246
- context,
247
- tr("Choose a device"),
248
- choices.map((device) => ({
249
- name: `${device.name} ${device.udid}`,
250
- short: device.name,
251
- value: device,
252
- })),
253
- failure,
254
- );
255
- }
256
-
257
- export const apps = async (device) =>
258
- device.virtual
259
- ? guestApps(device)
260
- : (
261
- await devicectl(["device", "info", "apps", "--device", device.udid], {
262
- parse: true,
263
- })
264
- ).result.apps;
265
-
266
- export const lockedError = /BSErrorCodeDescription = Locked|device is locked/;
267
-
268
- export const installApp = async (device, app, id) =>
269
- device.virtual
270
- ? guest(device, ["install", app, id])
271
- : devicectl(["device", "install", "app", "--device", device.udid, app]);
272
-
273
- export const uninstallApp = async (device, id) =>
274
- device.virtual
275
- ? callGuest(device, "apps.uninstall", { bundle_id: id, force: true })
276
- : devicectl(["device", "uninstall", "app", "--device", device.udid, id]);
277
-
278
- export const launchApp = async (device, id, args = []) =>
279
- device.virtual
280
- ? launchOnGuest(device, id)
281
- : devicectl([
282
- "device",
283
- "process",
284
- "launch",
285
- "--terminate-existing",
286
- "--device",
287
- device.udid,
288
- id,
289
- ...(args.length > 0 ? ["--", ...args] : []),
290
- ]);
291
-
292
- export async function terminateApp(device, id) {
293
- if (device.virtual) {
294
- await callGuest(device, "apps.terminate", { bundle_id: id });
295
- return;
296
- }
297
- const [listed, { result }] = await Promise.all([
298
- apps(device),
299
- devicectl(["device", "info", "processes", "--device", device.udid], {
300
- parse: true,
301
- }),
302
- ]);
303
- const { url } = listed.find((app) => app.bundleIdentifier === id);
304
- const running = result.runningProcesses.find((process) =>
305
- process.executable?.startsWith(url),
306
- );
307
- if (running !== undefined) {
308
- await devicectl([
309
- "device",
310
- "process",
311
- "terminate",
312
- "--device",
313
- device.udid,
314
- "--pid",
315
- String(running.processIdentifier),
316
- ]);
317
- }
318
- }
319
-
320
- async function guestApps(device) {
321
- const { apps: listed } = await callGuest(device, "apps.list");
322
- return listed.map((app) => ({
323
- bundleIdentifier: app.bundle_id,
324
- version: app.version,
325
- bundleVersion: app.build,
326
- }));
327
- }
328
-
329
- async function launchOnGuest(device, id) {
330
- const until = Date.now() + 30_000;
331
- while (true) {
332
- try {
333
- await callGuest(device, "screen.unlock");
334
- await callGuest(device, "apps.launch", { bundle_id: id });
335
- return;
336
- } catch (error) {
337
- if (Date.now() > until) {
338
- throw error;
339
- }
340
- }
341
- await new Promise((resolve) => setTimeout(resolve, 1_000));
342
- }
343
- }
344
-
345
- export const configureGuest = (device, id, name) =>
346
- guest(device, ["configure", id, name]);
347
-
348
- export async function coreDevice(context, options = {}) {
349
- const device = await findDevice(context, options);
350
- if (device.virtual) {
351
- await step(
352
- tr("pairing with {device}", { device: device.name }),
353
- () => guest(device, ["pair"]),
354
- { transient: true },
355
- );
356
- }
357
- return device;
358
- }
359
-
360
- async function guestContainer(device, id) {
361
- const { data_path: root } = await callGuest(device, "apps.data_dir", {
362
- bundle_id: id,
363
- });
364
- return root;
365
- }
366
-
367
- const containerOptions = (id) => [
368
- "--domain-type",
369
- "appDataContainer",
370
- "--domain-identifier",
371
- id,
372
- ];
373
-
374
- export async function containerHasFile(device, id, file) {
375
- if (device.virtual) {
376
- const root = await guestContainer(device, id);
377
- const directory = path.posix.join(root, path.posix.dirname(file));
378
- const { entries } = await callGuest(device, "files.list", {
379
- path: directory,
380
- }).catch(() => ({ entries: [] }));
381
- return entries.some((it) => it.name === path.posix.basename(file));
382
- }
383
- const { result } = await devicectl(
384
- [
385
- "device",
386
- "info",
387
- "files",
388
- "--device",
389
- device.udid,
390
- ...containerOptions(id),
391
- ],
392
- { parse: true },
393
- );
394
- return result.files.some((it) => it.relativePath === file);
395
- }
396
-
397
- export async function pullFromContainer(device, id, source, destination) {
398
- if (device.virtual) {
399
- const root = await guestContainer(device, id);
400
- const { content } = await callGuest(device, "files.read", {
401
- path: path.posix.join(root, source),
402
- binary: true,
403
- });
404
- fs.writeFileSync(destination, Buffer.from(content, "base64"));
405
- return;
406
- }
407
- await devicectl([
408
- "device",
409
- "copy",
410
- "from",
411
- "--device",
412
- device.udid,
413
- "--source",
414
- source,
415
- "--destination",
416
- destination,
417
- ...containerOptions(id),
418
- ]);
419
- }
420
-
421
- const containerOwner = "501:501";
422
-
423
- export async function pushToContainer(device, id, source, destination) {
424
- if (device.virtual) {
425
- const root = await guestContainer(device, id);
426
- const target = path.posix.join(root, destination);
427
- const directory = path.posix.dirname(target);
428
- await callGuest(device, "files.mkdir", { path: directory });
429
- await callGuest(device, "files.chown", {
430
- path: directory,
431
- owner: containerOwner,
432
- });
433
- await callGuest(device, "files.write", {
434
- path: target,
435
- content: fs.readFileSync(source).toString("base64"),
436
- encoding: "base64",
437
- });
438
- await callGuest(device, "files.chown", {
439
- path: target,
440
- owner: containerOwner,
441
- });
442
- return;
443
- }
444
- await devicectl([
445
- "device",
446
- "copy",
447
- "to",
448
- "--device",
449
- device.udid,
450
- "--source",
451
- source,
452
- "--destination",
453
- destination,
454
- ...containerOptions(id),
455
- ]);
456
- }
457
-
458
- export const provisioningProfiles = async (udid) =>
459
- (
460
- await devicectl(
461
- ["device", "profile", "list", "--type", "provisioning", "--device", udid],
462
- { parse: true },
463
- )
464
- ).result.provisioningProfiles;