asyx 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asyx",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Asyx Command Line Interface",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -2,11 +2,9 @@ import { constants as fsConstants } from 'node:fs';
2
2
  import { access } from 'node:fs/promises';
3
3
  import { join } from 'node:path';
4
4
  import { AsyxMqttClient, controlWildcard, controlOpen, controlClose, controlStatus } from "@asyx/mqtt-client";
5
- import { loadOrCreateClientId } from "../lib/device.js";
5
+ import { loadOrCreateClientId, getDeviceJsonPath } from "../lib/device.js";
6
6
  import { getSharedTransport } from "../lib/shared-transport.js";
7
7
 
8
- const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
9
-
10
8
  export default async function mqtt() {
11
9
  // Dev-only command gating
12
10
  const isDev = process.env.NODE_ENV === 'dev';
@@ -15,8 +13,9 @@ export default async function mqtt() {
15
13
  process.exit(2);
16
14
  }
17
15
 
18
- // Device credential guard
19
- await access(DEVICE_JSON_PATH, fsConstants.F_OK).catch(() => {
16
+ // Device credential guard - use same path resolution as device.js
17
+ const deviceJsonPath = getDeviceJsonPath();
18
+ await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
20
19
  console.error('This command is for dev use only. Missing ~/.asyx/device.json');
21
20
  process.exit(2);
22
21
  });
@@ -138,8 +137,9 @@ async function connect() {
138
137
  const { readFile } = await import('node:fs/promises');
139
138
  const { join: pathJoin } = await import('node:path');
140
139
 
141
- const home = process.env.HOME || process.env.USERPROFILE || ".";
142
- const devJson = await readFile(pathJoin(home, ".asyx", "device.json"), "utf8");
140
+ // Use same path resolution as device.js for consistency
141
+ const deviceJsonPath = getDeviceJsonPath();
142
+ const devJson = await readFile(deviceJsonPath, "utf8");
143
143
  const dev = JSON.parse(devJson);
144
144
  const clientId = dev.clientId;
145
145
  const cli = new AsyxMqttClient(t, { clientId });
@@ -6,14 +6,14 @@ import fssync from 'node:fs';
6
6
  import path from 'node:path';
7
7
  import os from 'node:os';
8
8
  import { ensureKeyAndCsr } from '../lib/csr.js';
9
- import { loadOrCreateClientId } from '../lib/device.js';
9
+ import { loadOrCreateClientId, getDeviceJsonPath } from '../lib/device.js';
10
10
 
11
11
  const STS_URL = process.env.ASYX_STS_URL || 'https://www.asyx.ai/www/sts';
12
- const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
13
12
 
14
13
  export default async function cmdSts(argv = process.argv.slice(2)) {
15
- // Device credential guard
16
- await access(DEVICE_JSON_PATH, fsConstants.F_OK).catch(() => {
14
+ // Device credential guard - use same path resolution as device.js
15
+ const deviceJsonPath = getDeviceJsonPath();
16
+ await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
17
17
  console.error('This command is for dev use only. Missing ~/.asyx/device.json');
18
18
  process.exit(2);
19
19
  });
@@ -9,8 +9,7 @@ import crypto from "node:crypto";
9
9
  import logger from "../lib/logger.js";
10
10
  import { dispatch as dispatchEvt } from "../mqtt/events/index.js";
11
11
  import { MessageIngress } from "../mqtt/ingress.js";
12
-
13
- const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
12
+ import { getDeviceJsonPath } from "../lib/device.js";
14
13
 
15
14
  /**
16
15
  * QPA-CLI-09: Publish refresh request with ECONNRESET error handling.
@@ -78,8 +77,9 @@ export async function publishRefreshRequestWithErrorHandling(
78
77
  }
79
78
 
80
79
  export default async function tunnel(args = []) {
81
- // Device credential guard
82
- await access(DEVICE_JSON_PATH, fsConstants.F_OK).catch(() => {
80
+ // Device credential guard - use same path resolution as device.js
81
+ const deviceJsonPath = getDeviceJsonPath();
82
+ await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
83
83
  console.error('This command is for dev use only. Missing ~/.asyx/device.json');
84
84
  process.exit(2);
85
85
  });
package/src/lib/device.js CHANGED
@@ -1,4 +1,4 @@
1
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
1
+ import * as fs from "fs";
2
2
  import { homedir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import crypto from "node:crypto";
@@ -26,18 +26,26 @@ const DIR = resolveConfigDir();
26
26
  const FILE = join(DIR, "device.json");
27
27
 
28
28
  export function loadOrCreateClientId() {
29
- if (!existsSync(DIR)) mkdirSync(DIR, { recursive: true });
30
- if (existsSync(FILE)) {
31
- const raw = JSON.parse(readFileSync(FILE, "utf8"));
29
+ if (!fs.existsSync(DIR)) fs.mkdirSync(DIR, { recursive: true });
30
+ if (fs.existsSync(FILE)) {
31
+ const raw = JSON.parse(fs.readFileSync(FILE, "utf8"));
32
32
  if (raw?.clientId) return raw.clientId;
33
33
  }
34
34
  const clientId = "cli_" + crypto.randomUUID().replace(/-/g, "").slice(0, 12).toUpperCase();
35
- writeFileSync(FILE, JSON.stringify({ clientId }, null, 2));
35
+ fs.writeFileSync(FILE, JSON.stringify({ clientId }, null, 2));
36
36
  return clientId;
37
37
  }
38
38
 
39
39
  export function resetClientId() {
40
- if (existsSync(FILE)) {
41
- writeFileSync(FILE, JSON.stringify({}, null, 2));
40
+ if (fs.existsSync(FILE)) {
41
+ fs.writeFileSync(FILE, JSON.stringify({}, null, 2));
42
42
  }
43
43
  }
44
+
45
+ /**
46
+ * Get the path to device.json using the same resolution logic as loadOrCreateClientId
47
+ * This ensures consistent path resolution across all commands (tunnel, setup, etc.)
48
+ */
49
+ export function getDeviceJsonPath() {
50
+ return FILE;
51
+ }