変奏現実

パソコンやMMORPGのことなどを思いつくまま・・・記載されている会社名・製品名・システム名などは、各社の商標、または登録商標です。

この画面は、簡易表示です

2025年4月10日

[node.js]node –inspect-wait

リモートホストで動くnode.jsのアプリ(index.js)をPCのchromeからデバッグできた。

# node --inspect-wait {ソース名}

で実行しchrome待ちになってるけど、Chromeの「chrome://inspect/」 のページの

Discover network targetsの【Configure】ボタンを押して

{リモートホストのIPアドレス}:9229

を追記しても、

Remote Target #{リモートホストのIPアドレス}

に index.js が表示されない。

man node でオプションを調べてみると

--inspect-wait=[host:]port
        Activate inspector on host:port and wait for debugger to be attached.

[host:]port ってIPアドレスやポートを指定できるんだ。

# node --inspect-wait={自分のIPアドレス}:{chromeと通信するポート番号} {ソース名}

で、chromeのDevToolsで、ファイル選択でindex.jsを選ぶと普通にデバッグできた。

但し、これはリモートホスト側でポート開放必須でinspectも何でも出来る様なので、

SSLでポートフォワードする方法の方がよさそう。

> ssh -L 9229:localhost:9229 {ユーザ名}@{リモートホストのIPアドレス}
$ node --inspect-wait index.js

これでChromeでデバッグしながら、ポートを解放せずにsshでソース修正ができるから結構使い道がありそうだし、chrome操作さけなら「うっかりソースのバグを修正していまう」コトも無い(ハズ

to 管理者:ポート開放して

from 管理者:無理

ってありそうだし(笑

最近のWindowsはkey-genできるしsshできるし便利になったね。(大笑

あ、BATファイルにすればいいなぁ

cmd /C ssh -L 9229:localhost:9229 {ユーザ名}@{リモートホストのIPアドレス}

あれ無限ループ?

ファイル名が悪かった(再起してた

cmd /C ssh -L 9229:localhost:9229 {ユーザ名}@{リモートホストのIPアドレス}

これでポートも繋がりすぐ node –inspect-wait index.js できる。

メデタシメデタシ

ps.2025/4/11

大元ネタはココらしい。



[xterm.js]ssh接続その3

WebSocketをxtermのアドオン@xterm/addon-attachに渡してるけど、このままではログをちょっとみたいとかできない。

@xterm/addon-attachから送信されるデータがいつもUint8Arrayなので、挟むコマンドはテキストで送ればいいのかと思ったら、クライアント側でstring, ArrayBuffer, Blobのどれをsendしようが、サーバ側にはUint8Arrayとして引き渡されていたので、サーバ側はいつもJSONデータが渡ってくる前提でコード。

/**
 * WebSocketクライアント(xterm.js)からメッセージ受信時の処理
 */
ws.on('message', async (event) => {
  // JSON.stringify()でテキストで送信しているハズ
  const textJson = await new Response(event).text();
  // JSONに成~れ!
  try {
    const json = JSON.parse(textJson);
    // JSONに成った!
    // sshかな?
    if (json.ssh) {
      if (typeof json.ssh === 'string') {
        console.log(`resv ssh text : '${json.ssh}'`);
        stream.write(json.ssh);
      } else if (json.ssh instanceof Uint8Array) {
        // いつものUint8Array
        const text = new TextDecoder().decode(json.ssh);
        console.log(`resv ssh binary : '${text}'`);
        stream.write(json.ssh);
      } else {
        console.log(`resv ssh unknown type[${typeof json.ssh}] : '${json.ssh}'`);
      }
    } else if (json.{その他1}) {
      // {その他1}かな?
      const resultText = {その他1}(json.{その他1});
      console.log(`{その他1}('${json.{その他1}}')\n='${resultText}'`);
      // 結果をJSONに置き換えて
      const responce = {
        {その他1}: resultText,
      };
      // 送信
      const blob = new Blob([JSON.stringify(responce)], { type: "application/json" });
      ws.send(blob);
    } else if (json.{その他2}) {
      ・・・省略・・・
    } else if (json.{その他n}) {
      ・・・ほぼ{その他1}と似た感じ
    } else  {
      // しらないコマンド
    }
  } catch (ex) {
    // 私はJSONに成れないのか!
    console.log(`resv not json's text : ex : '${ex}'`);
  }
});

クライアント側は、WebSocketのmessageメソッドに

webSocket.addEventListener("message", async (event) => {
 ・・・
  preventDefault();
}, { passive: false });

しても、xtermjs画面にデータを表示してしまう。

仕方が無い。

@xterm/addon-attachを外して・・・

自前でWebSocketを送受信する。(前途多難そう

/**
 * WebSocketの処理 ***************************
 */
/**
 * WebSocketのmessageイベント処理
 */
webSocket.addEventListener("message", async (event) => {
  // テキストにする
  const textJson = await new Response(event.data).text();
  // JSONに成~れ
  try {
    const json = JSON.parse(textJson);
    // 内容で分岐
    if (json.ssh) {
      const s = json.ssh;
      terminal.write("string" == typeof s ? s : new Uint8Array(s));
    } else if (json.{その他1}) {
      //  {その他1}のレスポンス
      console.log(`{その他1}='${json.{その他1}}'`);
    } else if (json.{その他2}) {
・・・
    } else if (json.{その他n}) {
      //  {その他n}のレスポンス
      console.log(`{その他n}='${json.{その他n}}'`);
    } else {
       // 知らないコマンド
    }
  } catch (ex) {
    // JSONに成れなかった
  }
});
/**
 * WebSocketのcloseイベント処理
 */
webSocket.addEventListener("close", ((event) => {
  terminal.write('*** disconnection ***');
  console.log('*** disconnection ***');
}));
/**
 * WebSocketのerrorイベント処理
 */
webSocket.addEventListener("error", ((event) => {
  terminal.write('*** socket error ***');
  console.error(`socket error : ${event}`);
}));
/**
 * Terminalのイベント処理
 */
/**
 * Terminalのdataイベント処理
 */
terminal.onData((event) => {
  // sshに送信するJSONに変換
  const msg = {
    ssh: event,
  };
  // テキストに展開しBlobで送信
  const blob = new Blob([JSON.stringify(msg)], { type: "application/json" });
  webSocket.send(blob);
});
/**
 * Terminalのbinaryイベント処理
 */
terminal.onBinary((event) => {
// 呼ばれてないので省略
});

@xterm/addon-attachを代行する処理は、

WebSocketのmessageの処理は長いけど(独自コードが多い

xtermjsのデータを送信するのは短くてよかった。

本当は色々チェックが必要なんだろうけど。(ま、いいか




top