execCli.mjs

import { spawn } from 'child_process'
import get from 'lodash-es/get.js'
import genPm from './genPm.mjs'
import delay from './delay.mjs'
import isarr from './isarr.mjs'
import isobj from './isobj.mjs'
import isfun from './isfun.mjs'
import isestr from './isestr.mjs'
import optVal from './_optVal.mjs'
import getOptReader from './getOptReader.mjs'
import buildOutputValidator from './buildOutputValidator.mjs'
import strTruncate from './strTruncate.mjs'
import getErrorMessage from './getErrorMessage.mjs'
import errToStr from './_errToStr.mjs'
import execProcessKillPid from './execProcessKillPid.mjs'
import execCliCore from './execCliCore.mjs'
import execSpawnError from './_execSpawnError.mjs'
import { unreadableMsg, snapArgs, snapEnv } from './_execSnap.mjs'
import callUser from './_callUser.mjs'
import isGenObj from './_isGenObj.mjs'
import getTrace from './getTrace.mjs'


//buildSpawnArgs, buildSpawnEnv, 共用核心已抽至execCliCore
let { buildSpawnArgs, buildSpawnEnv } = execCliCore


//opt類數值參數之規則(iseint, 帳本G17、R-OPT-VAL-01), 非預期改用預設
let RULE_MAX_RETRIES = { fn: 'execCli', name: 'opt.maxRetries', kind: 'eint', chk: { use: 'count' } }
let RULE_RETRY_DELAY_MS = { fn: 'execCli', name: 'opt.retryDelayMs', kind: 'eint', chk: { min: 1 } }
let RULE_TIMEOUT_MS = { fn: 'execCli', name: 'opt.timeoutMs', kind: 'eint', chk: { use: 'timer', min: 1 } }
let RULE_MAX_BUFFER = { fn: 'execCli', name: 'opt.maxBuffer', kind: 'eint', chk: { use: 'count', min: 1 } }
let RULE_EXIT_GRACE_MS = { fn: 'execCli', name: 'opt.exitGraceMs', kind: 'eint', chk: { use: 'timer', min: 1 } }


//pidOf, 觀察回調事件用之子進程pid(未能啟動者proc.pid為undefined, 事件欄位只放數字或null)
function pidOf(proc) {
    return typeof proc.pid === 'number' ? proc.pid : null
}


//kNoRetry, 無法啟動子進程之同步錯誤所產生之結果物件(確定性失敗, 重試無意義), 供execCli判定不重試, 不擴充對外之結果物件欄位
let kNoRetry = new WeakSet()


//optTruncate, 供strTruncate裁切失敗結果之stdout與stderr時, 於刪節號後標註原始總長度
let optTruncate = {
    funWithMsg: (str) => `(truncated, total ${str.length} chars)`,
}


/**
 * 單次非同步呼叫(內部使用, 不含重試邏輯)
 *
 * @param {String} command 輸入執行檔名稱字串
 * @param {Array} [args=[]] 輸入參數陣列
 * @param {Object} [opt={}] 輸入設定物件
 * @param {Function|null} [trace=null] 輸入觀察回調之通知函數(getTrace之回傳), 格式為(type, info)=>{}, 預設null代表不通知
 * @returns {Promise} 回傳Promise, resolve回傳結果物件
 */
function execCliOnce(command, args = [], opt = {}, trace = null) {

    //opt, 已由execCli前置校驗, 此處僅取預設值
    let {
        timeoutMs = 120000,
        cwd = process.cwd(),
        input = undefined,
        validate: validator = null, //已於parseInput經buildOutputValidator建立(驗證函數或null), 此處(含重試)不再判型呼叫端之值
        maxBuffer = 10 * 1024 * 1024,
        onStdout = undefined,
        onStderr = undefined,
        env: envExtra = undefined,
        keepFailOutput = false,
        exitGraceMs = null,
    } = opt

    //clip, 失敗結果之stdout、stderr: 預設截至n字元並註記原始總長, opt.keepFailOutput為true時保留已累積之全部(仍受maxBuffer限制)
    let clip = (s, n) => keepFailOutput === true ? s : strTruncate(s, n, optTruncate)

    //startTime
    let startTime = Date.now()

    //pm
    let pm = genPm()

    //spawn, 同步拋錯(EPERM、UNKNOWN、ENAMETOOLONG、參數檢查錯誤等)以結果物件回報而不reject, 且屬確定性失敗不重試; buildSpawnArgs(含不存在之.cmd/.bat)與buildSpawnEnv之拋錯亦同
    let proc
    try {

        //envSpawn, 併入process.env(同名以envExtra為準, 含PYTHONIOENCODING); 須在try內, env物件展開拋錯(如getter拋錯)亦以結果物件回報
        //每次attempt於此重新求值並僅作用於該次子進程, 不動本進程process.env, 故並行調用可各自帶不同值
        let envSpawn = buildSpawnEnv(envExtra)

        let winSpawn = buildSpawnArgs(command, args, { cwd, env: envSpawn })
        proc = spawn(winSpawn.file, winSpawn.args, {
            cwd,
            stdio: ['pipe', 'pipe', 'pipe'],
            windowsHide: true, //執行的主程序若沒有主控台調用執行程序就不會有視窗, 但若通過pm2執行會有, 須設定windowsHide=true
            env: envSpawn,
            ...winSpawn.opt,
        })
    }
    catch (err) {
        let e = execSpawnError(err, { cwd })
        let r = {
            ok: false,
            stdout: '',
            stderr: '',
            code: null,
            error: `${e.code}: ${e.message}`,
            durationMs: Date.now() - startTime,
            pid: undefined, //與'error'事件(如ENOENT)之結果一致: 未能啟動者帶pid鍵且其值為undefined(JSDoc契約)
        }
        kNoRetry.add(r)
        if (trace) {
            trace('settle', { reason: 'error', code: null, signal: null })
        }
        pm.resolve(r)
        return pm
    }

    //spawn, 子進程已建立(未能啟動者於其後由'error'事件回報, 此時pid為null)
    if (trace) {
        trace('spawn', { pid: pidOf(proc) })
    }

    let stdout = ''
    let stderr = ''

    //TextDecoder, 跨chunk邊界正確解碼UTF-8, 避免多位元組中文字元被切成亂碼(U+FFFD)
    //改用全域TextDecoder而非node內建string_decoder, 避免打包時多引入一個node builtin
    let stdoutDecoder = new TextDecoder('utf-8')
    let stderrDecoder = new TextDecoder('utf-8')

    let settled = false
    let timedOut = false
    let timer = null
    let graceTimer = null //opt.exitGraceMs: 子進程exit後等待close之寬限計時器
    let truncated = false //是否有輸出因maxBuffer上限被丟棄
    let finish = (r) => {
        if (truncated) {
            r.truncated = true //只於有丟棄時帶此鍵(未截斷之結果形狀同前版)
        }
        pm.resolve(r)
    }

    //error, 須於存取stdio前監聽: node對EMFILE、ENFILE不建立stdio即於nextTick發出'error', 先存取會拋TypeError且'error'無人監聽而拉倒程序
    //  可能為'找不到可執行檔'(ENOENT), '權限不足', '系統資源用盡'等, 訊息經execSpawnError去除參數值
    proc.on('error', (err) => {
        if (settled) {
            return
        }
        settled = true
        clearTimeout(timer)
        clearTimeout(graceTimer)
        let e = execSpawnError(err, { cwd })
        if (trace) {
            trace('settle', { reason: 'error', code: null, signal: null })
        }
        pm.resolve({
            ok: false,
            stdout: '',
            stderr: '',
            code: null,
            error: `${e.code}: ${e.message}`,
            durationMs: Date.now() - startTime,
            pid: proc.pid,
        })
    })

    //stdio, EMFILE、ENFILE時node未建立(undefined), 由上方error回報
    if (!proc.stdout || !proc.stderr || !proc.stdin) {
        return pm
    }

    //stdout data, 經TextDecoder跨chunk解碼; 回調經callUser, 拋錯(不攔截, 改於下一輪重拋)不致中斷累積
    proc.stdout.on('data', (chunk) => {
        let str = stdoutDecoder.decode(chunk, { stream: true })
        if (!str) {
            return
        }
        if (onStdout !== undefined) { //已於parseInput判型(函數或undefined), 監聽器內不再判型呼叫端之值(值於入口後撤銷時判型會於監聽器內拋出)
            callUser(onStdout, str)
        }
        let kept = stdout.length < maxBuffer
        if (kept) {
            stdout += str
        }
        else {
            truncated = true
        }
        if (trace && !settled) {
            trace('data', { stream: 'stdout', len: str.length, size: stdout.length, kept })
            if (kept && stdout.length >= maxBuffer) {
                trace('full', { stream: 'stdout', size: stdout.length })
            }
        }
    })

    //stderr data, 經TextDecoder跨chunk解碼
    proc.stderr.on('data', (chunk) => {
        let str = stderrDecoder.decode(chunk, { stream: true })
        if (!str) {
            return
        }
        if (onStderr !== undefined) { //同onStdout
            callUser(onStderr, str)
        }
        let kept = stderr.length < maxBuffer
        if (kept) {
            stderr += str
        }
        else {
            truncated = true
        }
        if (trace && !settled) {
            trace('data', { stream: 'stderr', len: str.length, size: stderr.length, kept })
            if (kept && stderr.length >= maxBuffer) {
                trace('full', { stream: 'stderr', size: stderr.length })
            }
        }
    })

    //stdin, 子進程提早關閉stdin時write/end會觸發EPIPE, 需listen'error'才不會拋unhandled event
    proc.stdin.on('error', () => {})
    if (input !== undefined) {
        proc.stdin.write(input, 'utf8')
    }
    proc.stdin.end()

    //timer, 逾時觸發時調用execProcessKillPid殺整棵程序樹
    timer = setTimeout(() => {
        if (settled) {
            return
        }
        timedOut = true
        if (trace) {
            trace('timeout', { ms: timeoutMs })
            trace('kill', { pid: pidOf(proc) })
        }

        //fire-and-forget, 殺掉子進程後close事件會接著觸發, 由timedOut決定回傳訊息
        execProcessKillPid(proc.pid)
            .catch(() => {}) //pid可能已結束, 忽略error

        //保險: kill後若close事件遲未觸發(kill被忽略/權限不足), 3s後強制resolve避免永久hang
        //unref確保正常情況(close立即觸發)不會因此延後process結束
        setTimeout(() => {
            if (settled) {
                return
            }
            settled = true
            clearTimeout(graceTimer)
            if (trace) {
                trace('settle', { reason: 'stuck', code: null, signal: null })
            }
            finish({
                ok: false,
                stdout: clip(stdout, 500),
                stderr: clip(stderr, 500),
                code: null,
                error: `TIMEOUT after ${timeoutMs / 1000}s(子進程未能結束)`,
                durationMs: Date.now() - startTime,
                pid: proc.pid,
            })
            //有opt.exitGraceMs者(呼叫端要求不因被握住之管道而不返回)亦銷毀管道; 本定案路徑會清掉寬限計時器
            if (exitGraceMs !== null) {
                destroyStdio()
            }
        }, 3000).unref()

    }, timeoutMs)

    //exitGrace, 子進程已exit而close遲未發生(其衍生之行程仍握著管道)時, 逾opt.exitGraceMs即以已收到之輸出定案並銷毀管道
    if (exitGraceMs !== null) {
        proc.on('exit', (code, signal) => {
            if (settled) {
                return
            }
            clearTimeout(timer) //子進程已結束, 不再以timeoutMs判逾時(已逾時者timedOut已為true)
            graceTimer = setTimeout(() => {
                if (settled) {
                    return
                }
                if (trace) {
                    trace('grace', { ms: exitGraceMs })
                }
                onClose(code, signal)
                destroyStdio()
            }, exitGraceMs)
        })
    }

    //destroyStdio, 銷毀子進程之管道(其衍生之行程仍握著時, 不銷毀會使本行程無法結束)
    let destroyStdio = () => {
        for (let s of [proc.stdin, proc.stdout, proc.stderr]) {
            try {
                s.destroy()
            }
            catch (err) {}
        }
    }

    //close
    let onClose = (code, signal) => {
        if (settled) {
            return
        }
        settled = true
        clearTimeout(timer)
        clearTimeout(graceTimer)

        //flush decoder殘餘(valid UTF-8結尾通常為空, 保險起見仍flush)
        let fOut = stdoutDecoder.decode()
        if (fOut) {
            if (stdout.length < maxBuffer) {
                stdout += fOut
            }
            else {
                truncated = true
            }
        }
        let fErr = stderrDecoder.decode()
        if (fErr) {
            if (stderr.length < maxBuffer) {
                stderr += fErr
            }
            else {
                truncated = true
            }
        }

        //durationMs
        let durationMs = Date.now() - startTime

        //timeout
        if (timedOut) {
            if (trace) {
                trace('settle', { reason: 'timeout', code, signal })
            }
            finish({
                ok: false,
                stdout: clip(stdout, 500),
                stderr: clip(stderr, 500),
                code,
                error: `TIMEOUT after ${timeoutMs / 1000}s`,
                durationMs,
                pid: proc.pid,
            })
            return
        }

        //code
        if (code !== 0) {
            if (trace) {
                trace('settle', { reason: 'exit', code, signal })
            }
            finish({
                ok: false,
                stdout: clip(stdout, 500),
                stderr: clip(stderr, 1000),
                code,
                error: signal ? `Signal: ${signal}` : `Exit code ${code}`,
                durationMs,
                pid: proc.pid,
            })
            return
        }

        //resolveOk
        let resolveOk = () => {
            if (trace) {
                trace('settle', { reason: 'ok', code, signal })
            }
            finish({
                ok: true,
                stdout,
                stderr,
                code: 0,
                error: '',
                durationMs,
                pid: proc.pid,
            })
        }

        //無驗證
        if (!validator) {
            resolveOk()
            return
        }

        //validator, 以原生Promise承接: 自訂函數同步拋錯或其Promise被reject視同驗證失敗(不成為未捕捉例外、不使本次呼叫懸置), 回傳Promise者以其結果判定, 真值為通過
        //拋錯訊息以'validate threw: 訊息'前綴於stderr(error仍恰為OUTPUT_VALIDATION_FAILED, 下游以此判定驗證失敗)
        //拋錯與否另以bThrew標記(拋出之值可為null、undefined, 不可拿值當旗標); 訊息取getErrorMessage, 為空者(null、undefined等)退為errToStr, 並截斷同stderr
        new Promise((resolve) => {
            resolve(validator(stdout))
        })
            .then((b) => {
                //generator物件(async function*或function*之回傳, 本體未執行)恆為真值, 視同驗證失敗
                if (isGenObj(b)) {
                    return { pass: false, bThrew: false, err: null, bGen: true }
                }
                return { pass: !!b, bThrew: false, err: null }
            }, (err) => {
                return { pass: false, bThrew: true, err }
            })
            .then(({ pass, bThrew, err, bGen }) => {
                if (pass) {
                    resolveOk()
                    return
                }
                let se = clip(stderr, 500)
                if (bGen) {
                    se = `invalid opt.validate: must not return a generator object${se !== '' ? `\n${se}` : ''}`
                }
                if (bThrew) {
                    let m = getErrorMessage(err)
                    if (m === '') {
                        m = errToStr(err)
                    }
                    m = strTruncate(m, 500, optTruncate)
                    se = `validate threw: ${m}${se !== '' ? `\n${se}` : ''}`
                }
                if (trace) {
                    trace('settle', { reason: 'validate', code, signal })
                }
                finish({
                    ok: false,
                    stdout: clip(stdout, 500),
                    stderr: se,
                    code: 0,
                    error: 'OUTPUT_VALIDATION_FAILED',
                    durationMs,
                    pid: proc.pid,
                })
            })

    }
    proc.on('close', onClose)

    return pm
}


/**
 * execCli之輸入判型與預設值(內部使用), 讀取或判型拋錯(getter拋錯、已撤銷之Proxy等不可讀之輸入)時直接拋出, 由execCli承接
 *
 * @param {*} command 輸入執行檔名稱
 * @param {*} args 輸入參數陣列
 * @param {*} opt 輸入設定物件
 * @returns {Object} 回傳{ rInvalid }(command無效之結果物件)或{ args, maxRetries, retryDelayMs, onceOpt, trace, stopRetryIf, beforeRetry }
 */
function parseInput(command, args, opt) {

    //command必填字串: 本函數以error物件回報失敗(非throw風格), 故回傳統一error結構
    if (!isestr(command)) {
        return {
            rInvalid: {
                ok: false,
                stdout: '',
                stderr: '',
                code: null,
                error: 'invalid command: must be a non-empty string',
                durationMs: 0,
                attempts: 0,
            },
        }
    }

    //command含NUL(空字元): 入口即擋下, 不交給spawn(node之原生訊息含參數名稱與值之描述), 同command無效不啟動、attempts 0、不帶pid鍵
    if (command.includes('\0')) {
        return {
            rInvalid: {
                ok: false,
                stdout: '',
                stderr: '',
                code: null,
                error: 'invalid command: must not contain a NUL character',
                durationMs: 0,
                attempts: 0,
            },
        }
    }

    //args: 若提供須為陣列, 否則回退為空陣列; 元素於此逐一讀取為快照(讀取拋錯者以'invalid args: must be a readable array'回報, 其後不再觸及呼叫端之陣列)
    if (!isarr(args)) {
        args = []
    }
    else {
        args = snapArgs(args)
    }

    //opt: 若提供須為物件, 否則回退為空物件
    if (!isobj(opt)) {
        opt = {}
    }

    let { maxRetries = 0, retryDelayMs = 5000, ...onceOpt } = opt

    //cwd: 非空字串, 無效回退預設process.cwd()
    let cwd = get(onceOpt, 'cwd', null)
    if (!isestr(cwd)) {
        onceOpt.cwd = process.cwd()
    }
    else {
        onceOpt.cwd = cwd
    }

    //input: 傳入stdin的字串, 無效回退預設undefined(未提供 = 不寫stdin)
    //  通過isestr者(含包裝物件new String)於此轉為原始字串: stdin.write只收原始字串, 包裝物件會於spawn後(本try外)同步拋錯而reject且子進程殘留; 轉字串拋錯者於此以結果物件回報
    let input = get(onceOpt, 'input', null)
    if (!isestr(input)) {
        onceOpt.input = undefined
    }
    else {
        onceOpt.input = String(input)
    }

    //env: 逐次注入之環境變數物件, 無效回退預設undefined(未提供 = 不覆寫任何變數)
    let env = get(onceOpt, 'env', null)
    if (!isobj(env)) {
        onceOpt.env = undefined
    }
    else {
        onceOpt.env = snapEnv(env) //於此展開為快照(讀取拋錯者以'invalid opt.env: must be a readable object'回報), 各次嘗試不再觸及呼叫端之物件
    }

    //validate: 驗證規則字串或自訂函數, 於此經buildOutputValidator判型並建立驗證函數一次(無有效規則為null = 不驗證), execCliOnce(含重試)直接使用而不再判型呼叫端之值
    let validate = get(onceOpt, 'validate', null)
    onceOpt.validate = buildOutputValidator(validate)

    //onStdout / onStderr: 串流回調函數, 無效回退預設undefined(未提供 = 不回調)
    let onStdout = get(onceOpt, 'onStdout', null)
    if (!isfun(onStdout)) {
        onceOpt.onStdout = undefined
    }
    else {
        onceOpt.onStdout = onStdout
    }
    let onStderr = get(onceOpt, 'onStderr', null)
    if (!isfun(onStderr)) {
        onceOpt.onStderr = undefined
    }
    else {
        onceOpt.onStderr = onStderr
    }

    //maxRetries、retryDelayMs、timeoutMs、maxBuffer: opt類數值參數以iseint判定(帳本G17、R-OPT-VAL-01), 非預期(含Infinity、小數、超出範圍、'0x10'、包裝物件)視為未給、改用預設, 不夾限; debug警告讀onceOpt(opt展開之副本: 呼叫端之存取器於展開時已各讀一次, 副本為資料屬性, 重讀無副作用, 符合V11; 同onTrace只認自有可列舉鍵)
    //  判定時只收集(optVal.defer), 業務鍵全部讀完後才至多讀一次debug(排程第437項①)
    let col = optVal.defer()

    //maxRetries: 有效整數且為個數(0~4294967295, 可為0), 無效回退預設0(Infinity不再視為近乎無限重試)
    maxRetries = col.take(maxRetries, 0, RULE_MAX_RETRIES)

    //retryDelayMs: 有效整數且大於等於1, 無效回退預設5000; 不設計時器上限(實際間隔為retryDelayMs乘以重試次數且夾至15000後才交給計時器)
    retryDelayMs = col.take(retryDelayMs, 5000, RULE_RETRY_DELAY_MS)

    //timeoutMs: 有效整數且為計時毫秒(1~2147483647), 無效(含超過計時器上限)回退預設120000, 不再夾至上限
    onceOpt.timeoutMs = col.take(get(onceOpt, 'timeoutMs', null), 120000, RULE_TIMEOUT_MS)

    //maxBuffer: 有效整數且為個數(字元數, 1~4294967295; 同stderrKeep用count, 超過者字串本已無法容納), 無效回退預設10MB
    onceOpt.maxBuffer = col.take(get(onceOpt, 'maxBuffer', null), 10 * 1024 * 1024, RULE_MAX_BUFFER)

    //exitGraceMs: 有效整數且為計時毫秒(1~2147483647), 未給或非預期為null(只以close定案, 同前版)
    onceOpt.exitGraceMs = col.take(get(onceOpt, 'exitGraceMs', null), null, RULE_EXIT_GRACE_MS)

    //keepFailOutput: 只有原始true生效(K3), 非布林視為false(opt.debug為true時警告)
    onceOpt.keepFailOutput = getOptReader('wsemi execCli', onceOpt)('keepFailOutput', false, (v) => typeof v === 'boolean', 'a boolean', 'false') === true

    //stopRetryIf、beforeRetry: 重試之接縫(排程第487項②), 函數, 非函數視為未給(同onStdout判型); 取自onceOpt(副本), 由execCli之重試迴圈呼叫, 不交execCliOnce
    let stopRetryIf = get(onceOpt, 'stopRetryIf', null)
    if (!isfun(stopRetryIf)) {
        stopRetryIf = null
    }
    let beforeRetry = get(onceOpt, 'beforeRetry', null)
    if (!isfun(beforeRetry)) {
        beforeRetry = null
    }

    //trace, 觀察回調(opt.onTrace), 取自onceOpt(opt展開之副本, 不再讀取呼叫端之opt), 置於其餘判型之後使既有失敗路徑不變; 未給時為null
    let trace = getTrace(onceOpt)

    //flush, 業務鍵全部讀完後才至多讀一次debug並警告非預期之數值鍵
    col.flush(onceOpt)

    return { args, maxRetries, retryDelayMs, onceOpt, trace, stopRetryIf, beforeRetry }
}


//callHook, 呼叫重試之接縫回調fn(...args)(不帶接收者), 一律以原生Promise承接(同validate): 回傳值可為一般值或Promise(等待其結果), 同步拋錯、reject、讀取回傳值之then拋錯皆不外拋, 回傳{ ok, v }(ok為false代表拋錯或reject)
//  回傳之Promise永不落定時本函數亦不落定(同validate, 由呼叫端負責; 文件載明)
function callHook(fn, ...args) {
    return new Promise((resolve) => {
        resolve(fn(...args))
    })
        .then((v) => {
            return { ok: true, v }
        }, () => {
            return { ok: false, v: undefined }
        })
}


//readBeforeRetry, 判讀opt.beforeRetry之結果: 物件之stop恰為true即中止, timeoutMs依opt.timeoutMs之同一規則(RULE_TIMEOUT_MS)判定, 有效即Number(值); 讀取拋錯、非物件、拋錯或reject者皆視為{}(照常以def啟動); 不警告(回調之回傳值非opt鍵)
function readBeforeRetry(h, def) {
    if (!h.ok || h.v === null || typeof h.v !== 'object') {
        return { stop: false, timeoutMs: def }
    }
    //stop, 先讀且獨立判定: 恰為true即中止, 不受timeoutMs之讀取結果影響(外審sol第2輪#04)
    let stop = false
    try {
        stop = h.v.stop === true
    }
    catch (err) {
        stop = false
    }
    if (stop) {
        return { stop: true, timeoutMs: def }
    }
    let timeoutMs = def
    try {
        timeoutMs = optVal(h.v.timeoutMs, def, RULE_TIMEOUT_MS)
    }
    catch (err) {
        timeoutMs = def
    }
    return { stop: false, timeoutMs }
}


/**
 * 通用CLI子程序調用,封裝逾時、進程樹清理、輸出驗證、結構化錯誤回報與自動重試
 * 各參數原樣傳遞給目標CLI,支援中文
 * Windows下npm全域安裝之命令為.cmd批次檔,Node.js spawn無法直接執行(CVE-2024-27980安全修正後回EINVAL),內部已參考cross-spawn自動解析.cmd入口並繞過cmd.exe,故可支援含多行文字之參數
 * 本函數不throw亦不reject,一律以結果物件之ok與error欄位回報成敗,error不含args與env之值(子程序自身之輸出另見stdout、stderr);opt或其選項值不可讀(getter拋錯、已撤銷之Proxy等)時亦以結果物件回報,error為固定訊息'invalid opt: must be a readable object'(不含呼叫端所拋出之文字與錯誤碼;子進程未能啟動,code為null、attempts為1),args之元素不可讀(含陣列Proxy之length非合法陣列長度或小於其自有可列舉索引鍵所示者)為'invalid args: must be a readable array',opt.env不可讀為'invalid opt.env: must be a readable object'(皆於入口讀為快照,其後不再觸及呼叫端之值);command含NUL(空字元)時不啟動,error為'invalid command: must not contain a NUL character'(attempts為0)
 *
 * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/execCli.test.mjs Github}
 * @memberOf wsemi
 * @param {String} command 輸入執行檔名稱字串,例如'node'、'curl'、'claude'等,可為註冊系統的全域指令或執行檔路徑
 * @param {Array} [args=[]] 輸入參數字串陣列,內容原樣傳遞給目標CLI,預設[]
 * @param {Object} [opt={}] 輸入設定物件
 * @param {Number} [opt.timeoutMs=120000] 輸入逾時毫秒正整數,須為有效整數且介於1至2147483647(計時器上限;iseint之use:'timer',可為字串型數字),逾時將調用execProcessKillPid強制關閉子進程及其子孫程序,非預期之輸入(含Infinity、小數、超過2147483647、'0x10'、包裝物件)視為未給而改用預設,不夾至上限、不報錯,預設120000
 * @param {String} [opt.cwd=process.cwd()] 輸入子進程工作目錄字串,預設process.cwd();不存在或非資料夾時以結果物件回報error為'ENOENT: invalid cwd: must be an existing folder'(未能啟動,不重試)
 * @param {String} [opt.input=undefined] 輸入傳入子進程stdin之字串,預設undefined代表不寫入stdin
 * @param {String|Function} [opt.validate=undefined] 輸入stdout驗證規則字串或自訂驗證函數,規則字串支援'nonempty'(非空)、'json'(可JSON.parse)、'min:100'(長度下限),多規則可用逗號串接例如'nonempty,json',自訂函數為(stdout)=>Boolean或回傳Promise(以其resolve值判定),回傳真值為通過;自訂函數拋錯或其Promise被reject視同驗證失敗(error為'OUTPUT_VALIDATION_FAILED',stderr前綴'validate threw: 錯誤訊息',訊息截斷至500字元);回傳generator物件(async function*或function*,本體未執行)亦視同驗證失敗(stderr前綴'invalid opt.validate: must not return a generator object');自訂函數回傳之Promise不受timeoutMs約束(子進程已結束),永不落定時本函數亦不落定,durationMs不含驗證時間,預設undefined代表不驗證
 * @param {Number} [opt.maxBuffer=10485760] 輸入stdout與stderr各自累積之最大字元數正整數,須為有效整數且介於1至4294967295(iseint之use:'count',可為字串型數字),以收到之輸出片段為單位截止: 累積量未達上限時該片段整段累積(故最後一段可使累積量略超過上限),達上限後之片段不再累積(此時結果帶truncated為true),非預期之輸入(含Infinity、小數、超過4294967295、'0x10'、包裝物件)視為未給而改用預設,欲近乎不限制請給足夠大之有限值,預設10485760(10MB)
 * @param {Boolean} [opt.keepFailOutput=false] 輸入失敗結果(非零離開碼、逾時、驗證失敗、樹殺後未能結束)之stdout、stderr是否保留已累積之全部布林值,須恰為true才生效;false時失敗結果截至500字元(非零離開碼之stderr為1000字元)並註記原始總長;保留者仍受maxBuffer限制;非布林視為false,預設false
 * @param {Number} [opt.exitGraceMs=null] 輸入子進程已exit而其管道遲未關閉(其衍生之行程仍握著stdout、stderr)時之寬限毫秒,有效整數1至2147483647(可為字串型數字);自exit起逾此時間仍未close即依exit之結果以已收到之輸出定案(成功仍經validate)並銷毀管道,其後衍生行程之輸出不再收;逾時後之強制定案(子進程未能結束)亦銷毀管道;給此鍵者子進程exit後即不再以timeoutMs判逾時;寬限須足以讀完子進程本身之輸出;未給或非預期視為未給(只以close定案),預設null
 * @param {Function} [opt.onStdout=undefined] 輸入stdout串流回調函數,格式為(chunk)=>{},回調內拋出之錯誤不會被攔截,將成為未捕捉例外,請於回調內自行處理(該錯誤於回調返回後立即(微任務)原樣重拋,不影響本函數之輸出累積與結果),預設undefined
 * @param {Function} [opt.onStderr=undefined] 輸入stderr串流回調函數,格式為(chunk)=>{},行為同onStdout,預設undefined
 * @param {Object} [opt.env=undefined] 輸入本次調用額外注入之環境變數物件,會併入process.env之後(同名以此為準,含PYTHONIOENCODING),僅作用於該次子進程而不影響本進程之process.env,故並行調用可各自帶不同值;值為undefined代表移除該變數(可遮蔽繼承值),值為null會成為字面字串'null'故建議勿傳;Windows下環境變數大小寫不敏感,同名不同大小寫(如Path與PATH)亦視為同名覆蓋,預設undefined代表不覆寫任何變數
 * @param {Number} [opt.maxRetries=0] 輸入失敗後最大重試次數非負整數,遇ENOENT(命令不存在、不存在之.cmd/.bat、npm shim之入口檔不存在)、exit code 2(參數錯誤)或無法啟動子進程之同步錯誤(如參數過長、權限不足、非有效執行檔、參數含空字元)視為不可重試而立即中止,須為有效整數且介於0至4294967295(iseint之use:'count',可為字串型數字),非預期之輸入(含Infinity、小數、負數、'0x10'、包裝物件)視為未給而改用預設0即不重試(Infinity不視為無限重試),欲多次重試請給有限值,預設0
 * @param {Number} [opt.retryDelayMs=5000] 輸入重試間隔毫秒正整數,須為有效整數且大於等於1(可為字串型數字),實際間隔為retryDelayMs乘以重試次數且上限15000ms,非預期之輸入(含Infinity、小數、'0x10'、包裝物件)視為未給而改用預設,預設5000
 * @param {Function} [opt.stopRetryIf=null] 輸入是否中止重試之判定函數,格式為(result, info)=>Boolean或回傳Promise(以其resolve值判定),於某次嘗試失敗而依規則將重試(非不可重試之錯誤且尚有重試次數)時,在發出retry事件與等待重試間隔之前呼叫並等待其結果;result為該次嘗試之結果物件之凍結淺複本(欄位同回傳之結果物件,attempts為至今之嘗試次數),info為凍結物件{ n(至今之嘗試次數,由1起算)、maxRetries(重試次數之採用值)、delayMs(若重試將等待之毫秒數)、timeoutMs(該次嘗試之逾時毫秒數) };結果恰為true時不再重試,以該次結果為最終結果(attempts為n,觀察回調之done事件reason為'stopped'),其他值、拋錯或reject皆視為繼續重試(不外拋、不使本函數reject);回傳之Promise不受timeoutMs約束,永不落定時本函數亦不落定(同validate,由呼叫端負責);不帶接收者呼叫;maxRetries為0、嘗試成功、遇不可重試之錯誤或重試次數已用完者不呼叫;非函數視為未給,預設null
 * @param {Function} [opt.beforeRetry=null] 輸入重試啟動前之回調函數,格式為(info)=>{ timeoutMs, stop }或回傳Promise(以其resolve值判定),於每次重試(第2次起之嘗試)等待重試間隔之後、發出attempt事件與啟動子進程之前呼叫並等待其結果,info為凍結物件{ n(即將啟動之嘗試序號,由2起算)、maxRetries(重試次數之採用值)、timeoutMs(opt.timeoutMs之採用值) };結果為物件時:stop恰為true則不啟動該次重試,以上次之結果為最終結果(attempts不變,done事件reason為'stopped'),timeoutMs依opt.timeoutMs之同一規則判定(1至2147483647之有效整數,可為字串型數字),有效即為該次重試之逾時、否則用opt.timeoutMs之採用值(不警告);結果非物件、讀取其鍵拋錯、拋錯或reject皆視為{}(照常以opt.timeoutMs啟動);供呼叫端於每次重試前依期限封頂逾時、重新排隊或中止;回傳之Promise不受timeoutMs約束,永不落定時本函數亦不落定;不帶接收者呼叫;未給時各次重試沿用opt.timeoutMs;非函數視為未給,預設null
 * @param {Boolean} [opt.debug=false] 輸入是否於opt.maxRetries、opt.retryDelayMs、opt.timeoutMs、opt.maxBuffer、opt.exitGraceMs、opt.keepFailOutput為非預期之輸入而改用預設時輸出警告布林值,須為true才生效,警告為固定詞彙(不含輸入值),同一參數每個行程只警告一次,預設false
 * @param {Function} [opt.funLog=null] 輸入警告輸出函數,格式為(msg, { fn, reason, key })=>{},未給則用console.warn,預設null
 * @param {Function} [opt.onTrace=null] 輸入觀察回調函數,於內部事件發生當下同步呼叫,傳入一個凍結之事件物件{ type, ... },供測試以事件(而非等待時間)同步與診斷;欄位皆為原始值,不含stdout與stderr之內文(只有長度);type為'attempt'時為即將啟動第n次嘗試(含n,由1起算),'retry'時為上次嘗試失敗而即將等待ms毫秒後啟動第n次嘗試(含n、ms;n由2起算,ms即實際間隔),'spawn'時為子進程已建立(含pid,未能啟動者為null),'data'時為收到一段已解碼之輸出(含stream為'stdout'或'stderr'、len為該段字元數、size為處理後該串流已累積之字元數、kept為該段是否被累積;累積量已達maxBuffer者kept為false且size不變;子進程結束時殘餘之解碼不通知,settle之後亦不通知),'full'時為該串流累積量首次達到maxBuffer(含stream、size),'timeout'時為timeoutMs逾時計時器觸發而即將樹殺(含ms為timeoutMs),'kill'時為即將以execProcessKillPid樹殺(含pid),'grace'時為子進程已exit而逾opt.exitGraceMs仍未close、即將定案(含ms為exitGraceMs;只於給了該鍵時),'settle'時為本次嘗試即將定案(含reason:'ok'為離開碼0且通過驗證,'exit'為非零離開碼或被訊號終止,'timeout'為逾時樹殺後close,'validate'為驗證失敗,'error'為子進程未能啟動,'stuck'為樹殺後3秒仍未收到close而強制定案;另含code為離開碼數字或null、signal為終止訊號字串或null),'done'時為整個呼叫即將回傳(含n為實際嘗試次數、ok、reason:成功為'ok',失敗為'spawn'(無法啟動子進程之同步錯誤)、'enoent'(命令不存在)、'code2'(離開碼2)等不可重試者,'stopped'為opt.stopRetryIf或opt.beforeRetry中止重試,其餘含maxRetries為0者為'exhausted');command無效或opt不可讀而在啟動前即回報者無事件;回調不帶接收者呼叫,其拋錯與回傳值皆忽略,不影響本函數,預設null
 * @returns {Promise} 回傳Promise,resolve回傳結果物件,內含ok(是否成功布林值)、stdout(標準輸出字串)、stderr(標準錯誤字串)、code(離開碼)、error(錯誤訊息字串,成功時為空字串)、durationMs(耗時毫秒)、pid(子進程pid)、attempts(實際嘗試次數)。子進程未能啟動時(spawn同步拋錯、'error'事件、不存在之.cmd/.bat、npm shim之入口檔不存在、cwd不存在、opt不可讀等)code為null且結果帶pid鍵、其值為undefined;已啟動者(含逾時、訊號結束、非零離開、驗證失敗)皆帶pid數值;command非非空字串或含NUL時不啟動,attempts為0且不帶pid鍵。任一串流有輸出因累積達maxBuffer而被丟棄時,結果另帶truncated為true(成功與失敗皆同);未丟棄者不帶此鍵。失敗結果之字元截斷(500、1000)不屬此類
 * @example
 * //need test in nodejs
 *
 * async function test() {
 *
 *     let nodeBin = process.execPath
 *
 *     let r1 = await execCli(nodeBin, ['-e', 'process.stdout.write("hello")'])
 *     console.log('r1', r1.ok, r1.stdout)
 *     // => r1 true hello
 *
 *     let r2 = await execCli(nodeBin, ['-e', 'process.exit(3)'])
 *     console.log('r2', r2.ok, r2.code, r2.error)
 *     // => r2 false 3 Exit code 3
 *
 *     let r3 = await execCli(nodeBin, ['-e', 'process.stdin.on("data",(d)=>process.stdout.write(d))'], { input: '中文測試' })
 *     console.log('r3', r3.ok, r3.stdout)
 *     // => r3 true 中文測試
 *
 *     let r4 = await execCli(nodeBin, ['-e', 'process.stdout.write("abc")'], { validate: 'json' })
 *     console.log('r4', r4.ok, r4.error)
 *     // => r4 false OUTPUT_VALIDATION_FAILED
 *
 *     let r5 = await execCli(nodeBin, ['-e', 'setTimeout(()=>{},30000)'], { timeoutMs: 500 })
 *     console.log('r5', r5.ok, r5.error)
 *     // => r5 false TIMEOUT after 0.5s
 *
 *     let r6 = await execCli(nodeBin, ['-e', 'process.stdout.write(process.env.API_KEY)'], { env: { API_KEY: 'KEY_A' } })
 *     console.log('r6', r6.ok, r6.stdout, process.env.API_KEY)
 *     // => r6 true KEY_A undefined (僅作用於該次子進程, 本進程process.env不受影響)
 *
 *     //失敗時保留完整輸出, 且超量時可由truncated得知(maxBuffer以片段為單位截止)
 *     let r7 = await execCli(nodeBin, ['-e', 'process.stdout.write("x".repeat(2000));process.exitCode=1'], { keepFailOutput: true })
 *     console.log('r7', r7.ok, r7.stdout.length, r7.truncated)
 *     // => r7 false 2000 undefined
 *
 *     //子進程已結束而其衍生行程仍握著管道: exit後寬限1秒即定案
 *     let sp = 'require("child_process").spawn(process.execPath,["-e","setTimeout(()=>{},5000)"],{stdio:["ignore","inherit","inherit"],detached:true}).unref();process.stdout.write("done")'
 *     let r8 = await execCli(nodeBin, ['-e', sp], { exitGraceMs: 1000 })
 *     console.log('r8', r8.ok, r8.stdout)
 *     // => r8 true done
 *
 * }
 * test()
 *     .catch((err) => {
 *         console.log(err)
 *     })
 *
 */
async function execCli(command, args = [], opt = {}) {

    //parseInput, 輸入之讀取與判型一律於此try內: 拋錯者(command、args、opt或其選項值之getter拋錯、已撤銷之Proxy等不可讀之輸入)以結果物件回報, 同spawn同步錯誤(code null、帶pid鍵且為undefined、attempts 1、不重試), 不reject
    let startTime = Date.now()
    let p
    try {
        p = parseInput(command, args, opt)
    }
    catch (err) {
        return {
            ok: false,
            stdout: '',
            stderr: '',
            code: null,
            error: unreadableMsg(err, 'invalid opt: must be a readable object'), //固定訊息(V19), 不含呼叫端getter所拋出之文字與錯誤碼(V20); 讀取呼叫端之值而可能拋錯者為opt、args之元素與opt.env(command以isestr、args以isarr判型皆不拋錯)
            durationMs: Date.now() - startTime,
            pid: undefined, //同spawn同步錯誤: 未能啟動者帶pid鍵且其值為undefined(JSDoc契約)
            attempts: 1,
        }
    }
    if (p.rInvalid) {
        return p.rInvalid
    }
    let { maxRetries, retryDelayMs, onceOpt, trace, stopRetryIf, beforeRetry } = p
    args = p.args

    let lastResult
    let totalAttempts = 0
    let stopped = false
    let attemptOpt = onceOpt //本次嘗試之設定(首次為onceOpt; 給了opt.beforeRetry且其回傳有效之timeoutMs時, 該次重試改用其逾時之副本)

    for (let attempt = 0; attempt <= maxRetries; attempt++) {

        //delay, 重試間隔隨次數遞增, 上限15000ms
        if (attempt > 0) {
            let ms = Math.min(retryDelayMs * attempt, 15000)

            //stopRetryIf, 上次嘗試失敗且可重試、尚有次數時, 於retry事件與等待之前判定是否中止(排程第487項②; 回傳Promise者等待其結果); 中止則以上次之結果為最終結果, done之reason為'stopped'
            if (stopRetryIf !== null) {
                let r = Object.freeze({ ...lastResult, attempts: totalAttempts })
                let info = Object.freeze({ n: totalAttempts, maxRetries, delayMs: ms, timeoutMs: attemptOpt.timeoutMs })
                let h = await callHook(stopRetryIf, r, info)
                if (h.ok && h.v === true) {
                    stopped = true
                    break
                }
            }

            if (trace) {
                trace('retry', { n: attempt + 1, ms })
            }
            await delay(ms)

            //beforeRetry, 等待之後、啟動之前(排程第487項②; 回傳Promise者等待其結果): 回傳{ stop: true }即不啟動、以上次之結果為最終結果(done之reason為'stopped'), 回傳有效之timeoutMs即為該次重試之逾時; 未給則各次沿用opt.timeoutMs
            if (beforeRetry !== null) {
                let info = Object.freeze({ n: attempt + 1, maxRetries, timeoutMs: onceOpt.timeoutMs })
                let b = readBeforeRetry(await callHook(beforeRetry, info), onceOpt.timeoutMs)
                if (b.stop) {
                    stopped = true
                    break
                }
                attemptOpt = b.timeoutMs === onceOpt.timeoutMs ? onceOpt : { ...onceOpt, timeoutMs: b.timeoutMs }
            }
        }

        if (trace) {
            trace('attempt', { n: attempt + 1 })
        }
        lastResult = await execCliOnce(command, args, attemptOpt, trace)
        totalAttempts = attempt + 1

        if (lastResult.ok) {
            lastResult.attempts = totalAttempts
            if (trace) {
                trace('done', { n: totalAttempts, ok: true, reason: 'ok' })
            }
            return lastResult
        }

        //不可重試的錯誤
        if (kNoRetry.has(lastResult)) {
            break //無法啟動子進程之同步錯誤(參數過長、權限不足、非有效執行檔、參數檢查錯誤等), 重試無意義
        }
        if (lastResult.error.includes('ENOENT')) {
            break //命令不存在, 重試無意義
        }
        if (lastResult.code === 2) {
            break //參數錯誤, 重試無意義
        }

    }

    lastResult.attempts = totalAttempts

    //done, 失敗而結束的原因(判定順序同上方迴圈內之不可重試判斷): 'stopped'為opt.stopRetryIf或opt.beforeRetry中止重試, 'spawn'為無法啟動子進程之同步錯誤, 'enoent'為命令不存在, 'code2'為離開碼2, 其餘(含maxRetries為0)為'exhausted'
    if (trace) {
        let reason = 'exhausted'
        if (stopped) {
            reason = 'stopped'
        }
        else if (kNoRetry.has(lastResult)) {
            reason = 'spawn'
        }
        else if (lastResult.error.includes('ENOENT')) {
            reason = 'enoent'
        }
        else if (lastResult.code === 2) {
            reason = 'code2'
        }
        trace('done', { n: totalAttempts, ok: false, reason })
    }

    return lastResult
}


export default execCli