asyx 0.10.1 → 0.11.1
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/AGENTS.md +42 -0
- package/bin/asyx.js +1 -1
- package/package.json +8 -2
- package/src/commands/account.js +80 -71
- package/src/commands/app.js +317 -151
- package/src/commands/in-use-messages.js +47 -48
- package/src/commands/mqtt-broker.js +36 -49
- package/src/commands/mqtt.js +111 -149
- package/src/commands/name.js +24 -22
- package/src/commands/reject-messages.js +123 -123
- package/src/commands/setup.js +107 -131
- package/src/commands/sts.js +45 -61
- package/src/commands/tunnel.js +853 -932
- package/src/commands/validate-target-host.js +36 -59
- package/src/lib/aws-iot-config.js +26 -24
- package/src/lib/certs.js +15 -18
- package/src/lib/csr.js +107 -182
- package/src/lib/device.js +26 -37
- package/src/lib/ids.js +1 -2
- package/src/lib/naming.js +74 -68
- package/src/lib/pairing.js +10 -11
- package/src/lib/shared-transport.js +5 -9
- package/src/main.js +63 -53
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +25 -287
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +0 -72
- package/vendor/tunnel-core/dist/control-plane/session.js +0 -20
- package/vendor/tunnel-core/dist/errors.js +0 -19
- package/vendor/tunnel-core/dist/flows/account.js +1 -6
- package/vendor/tunnel-core/dist/flows/goodbye.js +0 -4
- package/vendor/tunnel-core/dist/flows/http.js +24 -112
- package/vendor/tunnel-core/dist/flows/identity.js +17 -10
- package/vendor/tunnel-core/dist/flows/in-use.js +0 -6
- package/vendor/tunnel-core/dist/flows/reopen.js +0 -4
- package/vendor/tunnel-core/dist/flows/tcp.js +18 -84
- package/vendor/tunnel-core/dist/flows/udp.js +15 -52
- package/vendor/tunnel-core/dist/lib/fingerprint.js +38 -42
- package/vendor/tunnel-core/dist/lib/jwt-verifier.js +22 -74
- package/vendor/tunnel-core/dist/lib/logger.js +1 -9
- package/vendor/tunnel-core/dist/lib/refresh-helpers.js +1 -22
- package/vendor/tunnel-core/dist/lifecycle.js +1 -8
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +1 -53
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +10 -64
- package/vendor/tunnel-core/dist/mqtt/events/index.js +3 -36
- package/vendor/tunnel-core/dist/mqtt/events/schemas.js +0 -1
- package/vendor/tunnel-core/dist/mqtt/ingress.js +0 -60
- package/vendor/tunnel-core/dist/state/assignment.js +6 -122
- package/vendor/tunnel-core/dist/state/instance-registry.js +0 -63
- package/vendor/tunnel-core/dist/tcp-v1/framing.js +2 -64
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +64 -331
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.js +5 -14
- package/vendor/tunnel-core/dist/tunnel/reconnect.js +10 -50
- package/vendor/tunnel-core/dist/tunnel/request-sink.js +0 -16
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +32 -195
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +28 -153
- package/vendor/tunnel-core/dist/tunnel/ws-control.js +4 -39
- package/vendor/tunnel-core/dist/types.js +0 -2
- package/vendor/tunnel-core/dist/udp-v1/framing.js +2 -64
- package/vendor/tunnel-core/package.json +0 -25
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +0 -273
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +0 -79
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +0 -55
- package/vendor/tunnel-core/dist/errors.d.ts +0 -49
- package/vendor/tunnel-core/dist/flows/account.d.ts +0 -24
- package/vendor/tunnel-core/dist/flows/goodbye.d.ts +0 -10
- package/vendor/tunnel-core/dist/flows/http.d.ts +0 -61
- package/vendor/tunnel-core/dist/flows/identity.d.ts +0 -23
- package/vendor/tunnel-core/dist/flows/in-use.d.ts +0 -27
- package/vendor/tunnel-core/dist/flows/reopen.d.ts +0 -9
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +0 -85
- package/vendor/tunnel-core/dist/flows/udp.d.ts +0 -52
- package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +0 -2
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +0 -145
- package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +0 -27
- package/vendor/tunnel-core/dist/lib/logger.d.ts +0 -9
- package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +0 -16
- package/vendor/tunnel-core/dist/lifecycle.d.ts +0 -24
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +0 -56
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +0 -60
- package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +0 -1
- package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +0 -498
- package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +0 -63
- package/vendor/tunnel-core/dist/state/assignment.d.ts +0 -136
- package/vendor/tunnel-core/dist/state/instance-registry.d.ts +0 -88
- package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +0 -43
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +0 -140
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +0 -13
- package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +0 -76
- package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +0 -40
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +0 -92
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +0 -93
- package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +0 -22
- package/vendor/tunnel-core/dist/types.d.ts +0 -24
- package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +0 -41
|
@@ -1,56 +1,43 @@
|
|
|
1
1
|
import { AwsIotTransport } from "@asyx/mqtt-broker-aws";
|
|
2
2
|
import { controlWildcard } from "@asyx/mqtt-client";
|
|
3
|
-
|
|
4
3
|
export default async function mqttBroker() {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
4
|
+
const subcommand = process.argv[4];
|
|
5
|
+
if (!subcommand || subcommand === "help") {
|
|
6
|
+
console.log("MQTT Broker Commands:\n asyx mqtt broker:probe Probe AWS IoT Core configuration");
|
|
7
|
+
process.exit(0);
|
|
8
|
+
}
|
|
9
|
+
if (subcommand === "probe") {
|
|
10
|
+
await probe();
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
console.log(`Unknown broker subcommand: ${subcommand}`);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
async function probe() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
18
|
+
try {
|
|
19
|
+
console.log("🔍 Probing AWS IoT Core configuration...");
|
|
20
|
+
const transport = new AwsIotTransport();
|
|
21
|
+
await transport.connect({ clientId: "dummy" });
|
|
22
|
+
const config = transport.getConfig();
|
|
23
|
+
if (!config) {
|
|
24
|
+
throw new Error("Failed to load configuration");
|
|
25
|
+
}
|
|
26
|
+
console.log("\n📋 Configuration:");
|
|
27
|
+
console.log(` Endpoint: ${config.endpoint}`);
|
|
28
|
+
console.log(` Region: ${config.region}`);
|
|
29
|
+
console.log(` Client ID: ${config.clientId}`);
|
|
30
|
+
const topic = controlWildcard(config.clientId);
|
|
31
|
+
console.log(`\n📡 Control Topic:`);
|
|
32
|
+
console.log(` ${topic}`);
|
|
33
|
+
console.log("\n✅ Configuration valid (dry-run mode)");
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.error("❌ Configuration error:", error.message);
|
|
37
|
+
console.log("\n💡 Setup instructions:");
|
|
38
|
+
console.log(" export ASYX_MQTT_ENDPOINT='your-endpoint.iot.region.amazonaws.com'");
|
|
39
|
+
console.log(" export AWS_REGION='us-east-1'");
|
|
40
|
+
console.log(" export ASYX_CLIENT_ID='cli_ABC123DEF456'");
|
|
41
|
+
process.exit(1);
|
|
33
42
|
}
|
|
34
|
-
|
|
35
|
-
// Display configuration
|
|
36
|
-
console.log("\n📋 Configuration:");
|
|
37
|
-
console.log(` Endpoint: ${config.endpoint}`);
|
|
38
|
-
console.log(` Region: ${config.region}`);
|
|
39
|
-
console.log(` Client ID: ${config.clientId}`);
|
|
40
|
-
|
|
41
|
-
// Display resolved topic
|
|
42
|
-
const topic = controlWildcard(config.clientId);
|
|
43
|
-
console.log(`\n📡 Control Topic:`);
|
|
44
|
-
console.log(` ${topic}`);
|
|
45
|
-
|
|
46
|
-
console.log("\n✅ Configuration valid (dry-run mode)");
|
|
47
|
-
|
|
48
|
-
} catch (error) {
|
|
49
|
-
console.error("❌ Configuration error:", error.message);
|
|
50
|
-
console.log("\n💡 Setup instructions:");
|
|
51
|
-
console.log(" export ASYX_MQTT_ENDPOINT='your-endpoint.iot.region.amazonaws.com'");
|
|
52
|
-
console.log(" export AWS_REGION='us-east-1'");
|
|
53
|
-
console.log(" export ASYX_CLIENT_ID='cli_ABC123DEF456'");
|
|
54
|
-
process.exit(1);
|
|
55
|
-
}
|
|
56
43
|
}
|
package/src/commands/mqtt.js
CHANGED
|
@@ -4,161 +4,123 @@ import { join } from 'node:path';
|
|
|
4
4
|
import { AsyxMqttClient, controlWildcard, controlOpen, controlClose, controlStatus } from "@asyx/mqtt-client";
|
|
5
5
|
import { loadOrCreateClientId, getDeviceJsonPath } from "../lib/device.js";
|
|
6
6
|
import { getSharedTransport } from "../lib/shared-transport.js";
|
|
7
|
-
|
|
8
7
|
export default async function mqtt() {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
process.exit(1);
|
|
42
|
-
}
|
|
8
|
+
const isDev = process.env.NODE_ENV === 'dev';
|
|
9
|
+
if (!isDev) {
|
|
10
|
+
console.error('This command is reserved for internal dev use. Set NODE_ENV=dev to proceed.');
|
|
11
|
+
process.exit(2);
|
|
12
|
+
}
|
|
13
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
14
|
+
await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
|
|
15
|
+
console.error('This command is for dev use only. Missing ~/.asyx/device.json');
|
|
16
|
+
process.exit(2);
|
|
17
|
+
});
|
|
18
|
+
const subcommand = process.argv[3];
|
|
19
|
+
if (!subcommand || subcommand === "help") {
|
|
20
|
+
console.log("MQTT Commands:\n asyx mqtt listen Listen for control messages\n asyx mqtt inject Inject test messages (dev only)\n asyx mqtt broker AWS IoT Core broker commands");
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
if (subcommand === "listen") {
|
|
24
|
+
await listen();
|
|
25
|
+
}
|
|
26
|
+
else if (subcommand === "inject") {
|
|
27
|
+
await inject();
|
|
28
|
+
}
|
|
29
|
+
else if (subcommand === "broker") {
|
|
30
|
+
const { default: brokerCommand } = await import("./mqtt-broker.js");
|
|
31
|
+
await brokerCommand();
|
|
32
|
+
}
|
|
33
|
+
else if (subcommand === "connect") {
|
|
34
|
+
await connect();
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
console.log(`Unknown mqtt subcommand: ${subcommand}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
43
40
|
}
|
|
44
|
-
|
|
45
41
|
async function listen() {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const payload = JSON.parse(new TextDecoder().decode(message.payload));
|
|
65
|
-
console.log(JSON.stringify(payload));
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Keep process running until Ctrl-C
|
|
69
|
-
process.on('SIGINT', () => {
|
|
70
|
-
console.log('\nShutting down...');
|
|
71
|
-
unsubscribe();
|
|
72
|
-
process.exit(0);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// Keep alive
|
|
76
|
-
await new Promise(() => {});
|
|
42
|
+
const clientId = loadOrCreateClientId();
|
|
43
|
+
const topic = controlWildcard(clientId);
|
|
44
|
+
console.log(`listening on control ${clientId}`);
|
|
45
|
+
console.log(`topic: ${topic}`);
|
|
46
|
+
const transport = getSharedTransport();
|
|
47
|
+
const client = new AsyxMqttClient(transport, { clientId });
|
|
48
|
+
await client.core.connect({ clientId });
|
|
49
|
+
await client.subscribeControl();
|
|
50
|
+
const unsubscribe = client.onMessage((message) => {
|
|
51
|
+
const payload = JSON.parse(new TextDecoder().decode(message.payload));
|
|
52
|
+
console.log(JSON.stringify(payload));
|
|
53
|
+
});
|
|
54
|
+
process.on('SIGINT', () => {
|
|
55
|
+
console.log('\nShutting down...');
|
|
56
|
+
unsubscribe();
|
|
57
|
+
process.exit(0);
|
|
58
|
+
});
|
|
59
|
+
await new Promise(() => { });
|
|
77
60
|
}
|
|
78
|
-
|
|
79
61
|
async function inject() {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
62
|
+
let kind, port;
|
|
63
|
+
for (let i = 4; i < process.argv.length; i++) {
|
|
64
|
+
if (process.argv[i] === '--kind' && i + 1 < process.argv.length) {
|
|
65
|
+
kind = process.argv[i + 1];
|
|
66
|
+
}
|
|
67
|
+
else if (process.argv[i] === '--port' && i + 1 < process.argv.length) {
|
|
68
|
+
port = process.argv[i + 1];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (!kind || !port) {
|
|
72
|
+
console.log("Usage: NODE_ENV=dev asyx mqtt inject --kind open|close|status --port <port>");
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
if (kind !== 'open' && kind !== 'close' && kind !== 'status') {
|
|
76
|
+
console.error('kind must be "open", "close", or "status"');
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
const clientId = loadOrCreateClientId();
|
|
80
|
+
const transport = getSharedTransport();
|
|
81
|
+
const client = new AsyxMqttClient(transport, { clientId });
|
|
82
|
+
await client.core.connect({ clientId });
|
|
83
|
+
const command = {
|
|
84
|
+
type: `cmd/${kind}`,
|
|
85
|
+
port: parseInt(port, 10)
|
|
86
|
+
};
|
|
87
|
+
let controlTopic;
|
|
88
|
+
if (kind === 'open') {
|
|
89
|
+
controlTopic = controlOpen(clientId);
|
|
87
90
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
process.exit(1);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const clientId = loadOrCreateClientId();
|
|
101
|
-
|
|
102
|
-
// Create MQTT client with shared MemoryTransport
|
|
103
|
-
const transport = getSharedTransport();
|
|
104
|
-
const client = new AsyxMqttClient(transport, { clientId });
|
|
105
|
-
|
|
106
|
-
// Connect to the transport
|
|
107
|
-
await client.core.connect({ clientId });
|
|
108
|
-
|
|
109
|
-
// Publish synthetic control message
|
|
110
|
-
const command = {
|
|
111
|
-
type: `cmd/${kind}`,
|
|
112
|
-
port: parseInt(port, 10)
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
// Use the correct topic format that matches the client subscription
|
|
116
|
-
let controlTopic;
|
|
117
|
-
if (kind === 'open') {
|
|
118
|
-
controlTopic = controlOpen(clientId);
|
|
119
|
-
} else if (kind === 'close') {
|
|
120
|
-
controlTopic = controlClose(clientId);
|
|
121
|
-
} else if (kind === 'status') {
|
|
122
|
-
controlTopic = controlStatus(clientId);
|
|
123
|
-
}
|
|
124
|
-
await transport.publish({ topic: controlTopic, payload: JSON.stringify(command), qos: 0, retain: false });
|
|
125
|
-
|
|
126
|
-
console.log(`Injected: ${JSON.stringify(command)}`);
|
|
127
|
-
process.exit(0);
|
|
91
|
+
else if (kind === 'close') {
|
|
92
|
+
controlTopic = controlClose(clientId);
|
|
93
|
+
}
|
|
94
|
+
else if (kind === 'status') {
|
|
95
|
+
controlTopic = controlStatus(clientId);
|
|
96
|
+
}
|
|
97
|
+
await transport.publish({ topic: controlTopic, payload: JSON.stringify(command), qos: 0, retain: false });
|
|
98
|
+
console.log(`Injected: ${JSON.stringify(command)}`);
|
|
99
|
+
process.exit(0);
|
|
128
100
|
}
|
|
129
|
-
|
|
130
101
|
async function connect() {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
// print first incoming message then exit
|
|
157
|
-
const timer = setTimeout(() => { console.log("⌛ no response in 10s, exiting"); process.exit(0); }, 10000);
|
|
158
|
-
cli.onMessage((m) => {
|
|
159
|
-
console.log("📨 inbound:", m.topic, m.payload);
|
|
160
|
-
clearTimeout(timer);
|
|
161
|
-
process.exit(0);
|
|
162
|
-
});
|
|
163
|
-
return;
|
|
102
|
+
const { AwsIotTransport } = await import("@asyx/mqtt-broker-aws");
|
|
103
|
+
const { AsyxMqttClient, topics } = await import("@asyx/mqtt-client");
|
|
104
|
+
const t = new AwsIotTransport();
|
|
105
|
+
await t.connect();
|
|
106
|
+
const { readFile } = await import('node:fs/promises');
|
|
107
|
+
const { join: pathJoin } = await import('node:path');
|
|
108
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
109
|
+
const devJson = await readFile(deviceJsonPath, "utf8");
|
|
110
|
+
const dev = JSON.parse(devJson);
|
|
111
|
+
const clientId = dev.clientId;
|
|
112
|
+
const cli = new AsyxMqttClient(t, { clientId });
|
|
113
|
+
const wildcard = topics.controlWildcard(clientId);
|
|
114
|
+
await cli.subscribeControlWildcard();
|
|
115
|
+
console.log("✅ connected. subscribed:", wildcard);
|
|
116
|
+
const reqTopic = topics.controlCmdRequest(clientId);
|
|
117
|
+
await cli.publishRaw(reqTopic, JSON.stringify({ type: "cmd/request", port: 8000, when: Date.now() }));
|
|
118
|
+
console.log("📤 published request:", reqTopic);
|
|
119
|
+
const timer = setTimeout(() => { console.log("⌛ no response in 10s, exiting"); process.exit(0); }, 10000);
|
|
120
|
+
cli.onMessage((m) => {
|
|
121
|
+
console.log("📨 inbound:", m.topic, m.payload);
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
process.exit(0);
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
164
126
|
}
|
package/src/commands/name.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
// Story 2.1 r3: `asyx name check <name> [--json]` — can <name>.tunnel.asyx.ai be claimed?
|
|
2
|
-
// Works without setup or sign-in. Exit 0 available, 3 not available, 2 usage, 1 could not check.
|
|
3
1
|
import { checkName, describeAnswer, normalizeLabel, EXIT } from '../lib/naming.js';
|
|
4
|
-
|
|
5
2
|
export default async function cmdName(argv, { fetchImpl = fetch, out = console.log, err = console.error } = {}) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
3
|
+
if (argv[0] === 'add') {
|
|
4
|
+
const { cmdNameAdd } = await import('./account.js');
|
|
5
|
+
return cmdNameAdd(argv.slice(1), { out, err });
|
|
6
|
+
}
|
|
7
|
+
const json = argv.includes('--json');
|
|
8
|
+
const args = argv.filter((a) => a !== '--json');
|
|
9
|
+
if (args[0] !== 'check' || !args[1] || args.length > 2) {
|
|
10
|
+
err('Usage: asyx name check <name> [--json]\n asyx name add <name> [--json]');
|
|
11
|
+
return EXIT.USAGE;
|
|
12
|
+
}
|
|
13
|
+
const raw = args[1];
|
|
14
|
+
const label = normalizeLabel(raw) || String(raw).trim().toLowerCase();
|
|
15
|
+
try {
|
|
16
|
+
const answer = await checkName(label, { fetchImpl });
|
|
17
|
+
out(json ? JSON.stringify(answer) : describeAnswer(answer));
|
|
18
|
+
return answer.status === 'available' ? EXIT.OK : EXIT.NAME_UNAVAILABLE;
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
if (json)
|
|
22
|
+
out(JSON.stringify({ label, status: 'error', error: e.code || 'network_error', message: e.message }));
|
|
23
|
+
else
|
|
24
|
+
err(`Could not check the name: ${e.message}`);
|
|
25
|
+
return EXIT.FAILED;
|
|
26
|
+
}
|
|
25
27
|
}
|