fsWatchFolder.mjs

import path from 'path'
import chokidar from 'chokidar'
import get from 'lodash-es/get.js'
import isbol from './isbol.mjs'
import fsIsFolder from './fsIsFolder.mjs'
import evem from './evem.mjs'
import evEmit from './evEmit.mjs'
import getTrace from './getTrace.mjs'
import optVal from './_optVal.mjs'
import chkFsPath from './_chkFsPath.mjs'
import toResultError from './_toResultError.mjs'
import getDiag from './getDiag.mjs'
import pickDiagOpt from './pickDiagOpt.mjs'


//timeInterval、timeBinaryInterval: opt類參數, 有效整數且為正數計時毫秒(iseint, use: 'timer', min: 1; 交給chokidar, 其interval/binaryInterval經fs.watchFile須為uint32整數, 小數屬非預期; timeInterval亦交給setInterval), 非預期改用預設(100、300), 比照fsWatchFile
let RULE_TIME_INTERVAL = { fn: 'fsWatchFolder', name: 'opt.timeInterval', kind: 'eint', chk: { use: 'timer', min: 1 } }
let RULE_TIME_BINARY_INTERVAL = { fn: 'fsWatchFolder', name: 'opt.timeBinaryInterval', kind: 'eint', chk: { use: 'timer', min: 1 } }


/**
 * 後端nodejs基於chokidar提供偵測資料夾內檔案變更或出現或消失事件
 *
 * 因使用chokidar,變更至少要3秒才能監測,不適用於頻繁觸發事件之工作
 *
 * 停止監看(clear)時chokidar之unwatch同步拋錯或close被reject,記錄經getDiag(opt)之出口(R-DIAG-01規則14、20):預設以console.error只輸出固定詞彙之label'[wsemi fsWatchFolder] can not unwatch'或'[wsemi fsWatchFolder] can not close watcher'與安全摘要{ errType, errCode }(不輸出錯誤本體、堆疊與路徑);
 * opt.logDetail為true時改輸出遮罩後之細節;opt.funLog為函數時只交給它而不寫console;opt於建立時取快照。
 * 監聽器拋錯之通報同此出口(經evEmit,opt.logDetail、opt.funLog與上同一快照):有error監聽者時以error事件交給它(程式化通道,不經診斷出口),否則預設console.error'[wsemi fsWatchFolder] listener threw'與安全摘要,opt.logDetail為true時輸出遮罩後之細節,opt.funLog為函數時只交給它
 *
 * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/fsWatchFolder.test.mjs Github}
 * @memberOf wsemi
 * @param {String} fd 輸入偵測資料夾路徑字串
 * @param {Object} [opt={}] 輸入設定物件,預設{}
 * @param {Boolean} [opt.polling=false] 輸入是否使用輪循布林值,代表chokidar的usePolling,預設為false
 * @param {Integer} [opt.timeInterval=100] 輸入當polling為true時偵測檔案變更間隔時間整數,代表chokidar開啟polling時的interval,單位為毫秒ms,預設為100;亦為週期輪詢監看路徑是否仍為資料夾之間隔;須為1至2147483647之有效整數(數字字串亦可),非預期(0、負數、小數、Infinity、超過2147483647、'0x10'、包裝物件、非數字等)視為未給而改用預設100,不報錯、不夾至上限
 * @param {Integer} [opt.timeBinaryInterval=300] 輸入當polling為true時偵測二進位檔案變更間隔時間整數,代表chokidar開啟polling時的binaryInterval,單位為毫秒ms,預設為300;須為1至2147483647之有效整數(數字字串亦可),非預期(0、負數、小數、Infinity、超過2147483647、'0x10'、包裝物件、非數字等)視為未給而改用預設300,不報錯
 * @param {Boolean} [opt.debug=false] 輸入是否於opt.timeInterval或opt.timeBinaryInterval為非預期之輸入時輸出警告布林值,須為true才生效,警告為固定詞彙(不含輸入值),同一參數同一行程只警告一次,預設false
 * @param {Boolean} [opt.logDetail=false] 輸入是否於console輸出遮罩後之細節(停止監看錯誤、監聽器拋錯之通報),須為true才生效,同getDiag,建立時取快照,預設false(只輸出安全摘要)
 * @param {Function} [opt.funLog=null] 輸入診斷與警告之出口函數,格式為(label, safe, detail)=>{}:停止監看錯誤之記錄與監聽器拋錯之通報(無error監聽者時)以(label, { errType, errCode? }, detail)呼叫(同getDiag),opt.debug之警告以(msg, { fn, reason, key })呼叫;給予時皆只交給它而不寫console,未給則記錄寫console.error、警告寫console.warn,預設null
 * @param {Function} [opt.onTrace=null] 輸入觀察回調函數,於內部事件發生當下同步呼叫,傳入一個凍結之事件物件{ type, ... },供測試以事件(而非等待時間)同步與診斷;type為'tick'時為每次週期輪詢(每timeInterval一次;含watching為是否已有watcher、exists為監看路徑目前是否為資料夾),'watch'時為chokidar之watcher剛建立,'ready'時為chokidar初始掃描完成(僅傳了onTrace才監聽chokidar之ready,不影響原行為),'emit'時為即將對change監聽器派發一個事件(含kind為事件種類即change事件之type如add、change、unlink、addDir、unlinkDir,fp為path.resolve後之路徑字串,src為事件來源,'watcher'代表chokidar偵測到、'timer'代表週期輪詢發現監看資料夾已消失而合成之unlinkDir),'error'時為chokidar之watcher自身出錯(含fun為'watcher',錯誤物件不傳出,請由error事件取得),'unwatch'時為unWatch被呼叫且watcher尚存在(開始關閉;關閉完成前重複呼叫會重複通知),'closed'時為watcher.close()已完成且watcher已設為null(可再次建立watcher),'clear'時為clear被呼叫(僅首次);clear後僅會再通知unwatch與closed(資源釋放之回報),其餘事件不再通知;回調不帶接收者呼叫,其拋錯與回傳值皆忽略,不影響監看,預設null
 * @returns {Object} 回傳事件物件,包含on、clear函數,on可進行監聽change、error事件,clear為停止全部監聽,不須輸入。chokidar之watcher自身出錯(如資料夾內含無權限之子資料夾EPERM、EACCES)以error事件回報{ fun: 'watcher', msg }。事件物件為原生EventEmitter(eventemitter3)。本模組於派發處以try攔截監聽器之同步拋錯故其不會使行程崩潰;惟依EventEmitter規範,同一次派發中先拋錯之監聽器會中止該次派發,其後之監聽器不再被呼叫。async監聽器之reject不被攔截(規範上emit不觀察監聽器回傳值),須由監聽器自行處理,否則為unhandledRejection,監聽器出錯會改以error事件回報{ fun: 'listener', name, msg, args },監聽error時請先以fun欄位分流,路徑參數須為非空字串且不含NUL字元,無效時同步拋出Error(訊息如'invalid <參數名>: must be a non-empty string',不帶輸入值,與路徑不存在分開)
 * @example
 * need test in nodejs.
 *
 * import getFileName from './src/getFileName.mjs'
 * import fsDeleteFile from './src/fsDeleteFile.mjs'
 * import fsRenameFile from './src/fsRenameFile.mjs'
 * import fsCreateFolder from './src/fsCreateFolder.mjs'
 * import fsDeleteFolder from './src/fsDeleteFolder.mjs'
 * import fsRenameFolder from './src/fsRenameFolder.mjs'
 * import fsWatchFolder from './src/fsWatchFolder.mjs'
 *
 * let test = async () => {
 *     return new Promise((resolve, reject) => {
 *         let ms = []
 *
 *         let fp = './_test_fsWatchFolder'
 *
 *         fsDeleteFolder(fp)
 *
 *         let ev = fsWatchFolder(fp)
 *         ev.on('change', (msg) => {
 *             console.log(msg.type, getFileName(msg.fp))
 *             ms.push({ type: msg.type, fp: getFileName(msg.fp) })
 *         })
 *
 *         setTimeout(() => {
 *             fsCreateFolder(fp)
 *         }, 1)
 *
 *         setTimeout(() => {
 *             fs.writeFileSync(`${fp}/abc.txt`, 'abc', 'utf8')
 *         }, 3000)
 *
 *         setTimeout(() => {
 *             fsRenameFile(`${fp}/abc.txt`, `${fp}/abc.txt` + '.tmp')
 *         }, 6000)
 *
 *         setTimeout(() => {
 *             fsRenameFile(`${fp}/abc.txt` + '.tmp', `${fp}/abc.txt`)
 *         }, 9000)
 *
 *         setTimeout(() => {
 *             fs.writeFileSync(`${fp}/abc.txt`, 'def', 'utf8')
 *         }, 12000)
 *
 *         setTimeout(() => {
 *             fsCreateFolder(`${fp}/test-fd`)
 *         }, 15000)
 *
 *         setTimeout(() => {
 *             fsRenameFolder(`${fp}/test-fd`, `${fp}/test-fd` + '-tmp')
 *         }, 18000)
 *
 *         setTimeout(() => {
 *             fsRenameFolder(`${fp}/test-fd` + '-tmp', `${fp}/test-fd`)
 *         }, 21000)
 *
 *         setTimeout(() => {
 *             fsDeleteFile(`${fp}/abc.txt`)
 *         }, 24000)
 *
 *         setTimeout(() => {
 *             fsDeleteFolder(`${fp}/test-fd`)
 *         }, 27000)
 *
 *         setTimeout(() => {
 *             fsDeleteFolder(fp)
 *         }, 30000)
 *
 *         setTimeout(() => {
 *             ev.clear()
 *             console.log('ms', ms)
 *             resolve(ms)
 *         }, 33000)
 *
 *     })
 * }
 * await test()
 *     .catch((err) => {
 *         console.log(err)
 *     })
 * // addDir _test_fsWatchFolder
 * // add abc.txt
 * // unlink abc.txt
 * // add abc.txt.tmp
 * // unlink abc.txt.tmp
 * // add abc.txt
 * // change abc.txt
 * // addDir test-fd
 * // unlinkDir test-fd
 * // addDir test-fd-tmp
 * // unlinkDir test-fd-tmp
 * // addDir test-fd
 * // unlink abc.txt
 * // unlinkDir test-fd
 * // unlinkDir _test_fsWatchFolder
 * // ms [
 * //   { type: 'addDir', fp: '_test_fsWatchFolder' },
 * //   { type: 'add', fp: 'abc.txt' },
 * //   { type: 'unlink', fp: 'abc.txt' },
 * //   { type: 'add', fp: 'abc.txt.tmp' },
 * //   { type: 'unlink', fp: 'abc.txt.tmp' },
 * //   { type: 'add', fp: 'abc.txt' },
 * //   { type: 'change', fp: 'abc.txt' },
 * //   { type: 'addDir', fp: 'test-fd' },
 * //   { type: 'unlinkDir', fp: 'test-fd' },
 * //   { type: 'addDir', fp: 'test-fd-tmp' },
 * //   { type: 'unlinkDir', fp: 'test-fd-tmp' },
 * //   { type: 'addDir', fp: 'test-fd' },
 * //   { type: 'unlink', fp: 'abc.txt' },
 * //   { type: 'unlinkDir', fp: 'test-fd' },
 * //   { type: 'unlinkDir', fp: '_test_fsWatchFolder' }
 * // ]
 *
 */
function fsWatchFolder(fd, opt = {}) {

    //check, 路徑須為非空字串(無效與不存在分開判定, 不走到fs, 不回顯輸入值; R-FS-INVALID-PATH-01)
    let msgFd = chkFsPath(fd, 'fd')
    if (msgFd) {
        throw new Error(msgFd)
    }

    //polling
    let polling = get(opt, 'polling')
    if (!isbol(polling)) {
        polling = false
    }

    //timeInterval, 有效則取Number(v), 非預期(含Infinity、超出計時器上限、小數)改用預設, 不夾至上限
    let timeInterval = optVal(get(opt, 'timeInterval'), 100, RULE_TIME_INTERVAL, opt)

    //timeBinaryInterval, 同timeInterval
    let timeBinaryInterval = optVal(get(opt, 'timeBinaryInterval'), 300, RULE_TIME_BINARY_INTERVAL, opt)

    //ev
    let ev = evem() //change事件於chokidar回呼或setInterval內派發, 監聽器出錯不得殺行程, 故派發處以evEmit攔截

    //fpSpe
    let fpSpe = fd

    //trace, 觀察回調(opt.onTrace), 未給時為null
    let trace = getTrace(opt)

    //optDiag, opt.logDetail、opt.funLog之快照(各讀一次; 停止監看錯誤之診斷與監聽器拋錯之通報〔evEmit〕共用, R-DIAG-01規則14、20)
    let optDiag = pickDiagOpt(opt)

    //diag, 診斷輸出出口(停止監看錯誤不原樣console輸出; 永不拋錯)
    let diag = getDiag(optDiag)

    //cleared, 是否已clear, 供trace於clear後不再通知(unwatch、closed除外)
    let cleared = false

    //rootGone, 被監看之資料夾本身消失之unlinkDir是否已派發: 輪詢(timer)與chokidar皆可能偵測到該次消失, 而unWatch為延後(setTimeout 1ms)且close為非同步(watcher於其完成後才置null),
    //  期間下一次輪詢(輪詢回呼內耗時超過timeInterval時, 其到期時刻早於延後之unWatch而先執行)或chokidar之unlinkDir會重複派發, 故一次消失只派發一次, 建立新watcher時歸零
    let rootGone = false

    //timer
    let watcher = null
    let t = setInterval(() => {

        //exists, 每次輪詢皆查一次監看路徑是否為資料夾
        let exists = fsIsFolder(fpSpe)
        if (trace) {
            trace('tick', { watching: watcher !== null, exists })
        }

        //check
        if (watcher !== null) {

            //check, 若watcher已成功監聽fd, 但更名fd時會無法觸發
            if (!exists && !rootGone) {
                rootGone = true //一次消失只派發一次, 見rootGone

                //因監聽觸發狀態不能unWatch, 故要延遲呼叫

                //fpr
                let fpr = path.resolve(fpSpe)

                //emit, 資料夾已消失無法stat, stats比照chokidar之unlinkDir給undefined(原碼誤將fs.fstatSync函數本身塞入)
                if (trace) {
                    trace('emit', { kind: 'unlinkDir', fp: fpr, src: 'timer' })
                }
                evEmit(ev, 'change', [{
                    type: 'unlinkDir',
                    fp: fpr,
                    stats: undefined,
                }], { tag: 'fsWatchFolder', ...optDiag })

                setTimeout(() => {
                    unWatch()
                }, 1)
            }

            return
        }

        //check
        if (!exists) {
            return
        }

        //rootGone, 新watcher之生命週期重新計算
        rootGone = false

        //watcher
        watcher = chokidar.watch(fpSpe, {
            // persistent: true,
            // ignoreInitial: false,
            usePolling: polling,
            pollingInterval: timeInterval, //chokidar 6起更名(舊名interval保留別名)
            pollingBinaryInterval: timeBinaryInterval, //chokidar 6起更名(舊名binaryInterval保留別名)
            awaitWriteFinish: true, //須比較多延遲偵測檔案是否變更完成, 但對於連鎖驅動比較保險
            // depth: undefined,
        })

        //on
        watcher
            .on('all', (type, fp, stats) => {
                // console.log(type, fp, stats)
                //type=add,unlink,change,addDir,unlinkDir
                //注意當unlinkDir原本監聽的fd後, 就不會再觸發變更事件, 故外層用timer重新偵測

                //fp
                fp = path.resolve(fp)

                //isRoot, 是否為被監看之資料夾本身之unlinkDir; 該次消失已由輪詢派發則不重複派發, 否則標記為已派發(見rootGone)
                let isRoot = type === 'unlinkDir' && path.resolve(fpSpe) === fp
                if (isRoot) {
                    if (rootGone) {
                        return
                    }
                    rootGone = true
                }

                //emit
                if (trace && !cleared) {
                    trace('emit', { kind: type, fp, src: 'watcher' })
                }
                evEmit(ev, 'change', [{ type, fp, stats }], { tag: 'fsWatchFolder', ...optDiag })

                //check
                if (isRoot) {
                    //因監聽觸發狀態不能unWatch, 故要延遲呼叫
                    setTimeout(() => {
                        unWatch()
                    }, 1)
                }

            })
            .on('error', (err) => {
                //chokidar之FSWatcher為nodejs原生EventEmitter, 其對非ENOENT/ENOTDIR之錯誤(如EPERM、EACCES)會emit('error'), 無監聽者即throw殺行程, 故轉為ev之error事件
                if (trace && !cleared) {
                    trace('error', { fun: 'watcher' })
                }
                evEmit(ev, 'error', [{ fun: 'watcher', msg: toResultError(err) }], { tag: 'fsWatchFolder', ...optDiag })
            })

        //trace, 僅傳了onTrace才監聽ready與通知watch, 不影響原行為
        if (trace) {
            watcher.on('ready', () => {
                if (!cleared) {
                    trace('ready', {})
                }
            })
            trace('watch', {})
        }


    }, timeInterval)

    //unWatch
    let unWatch = () => {
        // console.log('call unWatch')
        if (watcher === null) {
            return
        }
        if (trace) {
            trace('unwatch', {})
        }
        try {
            watcher.unwatch(fpSpe)
        }
        catch (err) {
            diag('[wsemi fsWatchFolder] can not unwatch', diag.brief(err), () => err, 'error')
        }
        watcher.close()
            // .then(() => {})
            .catch((err) => {
                diag('[wsemi fsWatchFolder] can not close watcher', diag.brief(err), () => err, 'error')
            })
            .finally(() => {
                watcher = null
                // console.log('watcher=null')
                if (trace) {
                    trace('closed', {})
                }
            })
    }

    //clear
    let clear = () => {
        let first = !cleared
        cleared = true
        if (trace && first) {
            trace('clear', {})
        }
        unWatch()
        clearInterval(t)
    }

    //save
    ev.clear = clear

    return ev
}

export default fsWatchFolder