@marktrue/dsh-mobile-remote 0.0.0-stage → 3.2.1-alpha.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/CHANGELOG.md +796 -0
- package/CONTRIBUTING.md +59 -0
- package/FAQ.md +111 -0
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/cordis.patch.yml +14 -0
- package/docs/00-/345/274/200/345/217/221/346/200/273/347/272/262.md +122 -0
- package/docs/01-PRD.md +148 -0
- package/docs/02-architecture.md +249 -0
- package/docs/03-api.md +816 -0
- package/docs/04-security.md +96 -0
- package/docs/05-test-cases.md +326 -0
- package/docs/06-install-run.md +308 -0
- package/docs/07-user-manual.md +65 -0
- package/docs/08-/346/211/251/345/261/225/345/274/200/345/217/221/346/214/207/345/215/227.md +86 -0
- package/docs/09-compatibility.md +195 -0
- package/docs/10-release.md +927 -0
- package/docs/screenshots/01-home.png +0 -0
- package/docs/screenshots/02-notifications.png +0 -0
- package/docs/screenshots/03-sessions.png +0 -0
- package/docs/screenshots/04-chat.png +0 -0
- package/docs/screenshots/05-settings.png +0 -0
- package/docs/screenshots/06-new-session.png +0 -0
- package/docs/screenshots/pgy-appstore.jpg +0 -0
- package/docs/screenshots/pgy-mobile-app.jpg +0 -0
- package/docs/screenshots/pgy-pc-client.jpg +0 -0
- package/lib/account-usage.js +145 -0
- package/lib/app-alignment.js +112 -0
- package/lib/app-carrier-store.js +1972 -0
- package/lib/app-carrier.js +602 -0
- package/lib/client.js +706 -0
- package/lib/desktop-form.js +738 -0
- package/lib/endpoint-registry.js +162 -0
- package/lib/git-read-bundled-provider.js +255 -0
- package/lib/git-read-service.js +322 -0
- package/lib/index.js +9292 -0
- package/lib/plugin-content-digest.js +80 -0
- package/lib/plugin-self-update.js +758 -0
- package/lib/plugin-update-controller.js +95 -0
- package/lib/plugin-update-transaction.js +322 -0
- package/lib/plugin-update.js +309 -0
- package/lib/runtime-dependency-snapshot.js +345 -0
- package/lib/session-title-cache.js +123 -0
- package/lib/session-title-refresh.js +261 -0
- package/lib/update-fetch.js +24 -0
- package/lib/update-lock-arbiter.js +158 -0
- package/lib/update-supervisor.js +312 -0
- package/node_modules/ansi-regex/index.d.ts +37 -0
- package/node_modules/ansi-regex/index.js +10 -0
- package/node_modules/ansi-regex/license +9 -0
- package/node_modules/ansi-regex/package.json +55 -0
- package/node_modules/ansi-regex/readme.md +78 -0
- package/node_modules/ansi-styles/index.d.ts +345 -0
- package/node_modules/ansi-styles/index.js +163 -0
- package/node_modules/ansi-styles/license +9 -0
- package/node_modules/ansi-styles/package.json +56 -0
- package/node_modules/ansi-styles/readme.md +152 -0
- package/node_modules/camelcase/index.d.ts +63 -0
- package/node_modules/camelcase/index.js +76 -0
- package/node_modules/camelcase/license +9 -0
- package/node_modules/camelcase/package.json +43 -0
- package/node_modules/camelcase/readme.md +99 -0
- package/node_modules/cliui/CHANGELOG.md +76 -0
- package/node_modules/cliui/LICENSE.txt +14 -0
- package/node_modules/cliui/README.md +115 -0
- package/node_modules/cliui/index.js +354 -0
- package/node_modules/cliui/package.json +65 -0
- package/node_modules/color-convert/CHANGELOG.md +54 -0
- package/node_modules/color-convert/LICENSE +21 -0
- package/node_modules/color-convert/README.md +68 -0
- package/node_modules/color-convert/conversions.js +839 -0
- package/node_modules/color-convert/index.js +81 -0
- package/node_modules/color-convert/package.json +48 -0
- package/node_modules/color-convert/route.js +97 -0
- package/node_modules/color-name/LICENSE +8 -0
- package/node_modules/color-name/README.md +11 -0
- package/node_modules/color-name/index.js +152 -0
- package/node_modules/color-name/package.json +28 -0
- package/node_modules/decamelize/index.js +13 -0
- package/node_modules/decamelize/license +21 -0
- package/node_modules/decamelize/package.json +38 -0
- package/node_modules/decamelize/readme.md +48 -0
- package/node_modules/dijkstrajs/.travis.yml +4 -0
- package/node_modules/dijkstrajs/CONTRIBUTING.md +8 -0
- package/node_modules/dijkstrajs/LICENSE.md +19 -0
- package/node_modules/dijkstrajs/README.md +18 -0
- package/node_modules/dijkstrajs/dijkstra.js +165 -0
- package/node_modules/dijkstrajs/package.json +60 -0
- package/node_modules/dijkstrajs/test/dijkstra.test.js +96 -0
- package/node_modules/emoji-regex/LICENSE-MIT.txt +20 -0
- package/node_modules/emoji-regex/README.md +73 -0
- package/node_modules/emoji-regex/es2015/index.js +6 -0
- package/node_modules/emoji-regex/es2015/text.js +6 -0
- package/node_modules/emoji-regex/index.d.ts +23 -0
- package/node_modules/emoji-regex/index.js +6 -0
- package/node_modules/emoji-regex/package.json +50 -0
- package/node_modules/emoji-regex/text.js +6 -0
- package/node_modules/find-up/index.d.ts +137 -0
- package/node_modules/find-up/index.js +89 -0
- package/node_modules/find-up/license +9 -0
- package/node_modules/find-up/package.json +53 -0
- package/node_modules/find-up/readme.md +156 -0
- package/node_modules/get-caller-file/LICENSE.md +6 -0
- package/node_modules/get-caller-file/README.md +41 -0
- package/node_modules/get-caller-file/index.d.ts +2 -0
- package/node_modules/get-caller-file/index.js +22 -0
- package/node_modules/get-caller-file/index.js.map +1 -0
- package/node_modules/get-caller-file/package.json +42 -0
- package/node_modules/is-fullwidth-code-point/index.d.ts +17 -0
- package/node_modules/is-fullwidth-code-point/index.js +50 -0
- package/node_modules/is-fullwidth-code-point/license +9 -0
- package/node_modules/is-fullwidth-code-point/package.json +42 -0
- package/node_modules/is-fullwidth-code-point/readme.md +39 -0
- package/node_modules/locate-path/index.d.ts +83 -0
- package/node_modules/locate-path/index.js +65 -0
- package/node_modules/locate-path/license +9 -0
- package/node_modules/locate-path/package.json +45 -0
- package/node_modules/locate-path/readme.md +122 -0
- package/node_modules/p-limit/index.d.ts +38 -0
- package/node_modules/p-limit/index.js +57 -0
- package/node_modules/p-limit/license +9 -0
- package/node_modules/p-limit/package.json +52 -0
- package/node_modules/p-limit/readme.md +101 -0
- package/node_modules/p-locate/index.d.ts +64 -0
- package/node_modules/p-locate/index.js +52 -0
- package/node_modules/p-locate/license +9 -0
- package/node_modules/p-locate/package.json +53 -0
- package/node_modules/p-locate/readme.md +90 -0
- package/node_modules/p-try/index.d.ts +39 -0
- package/node_modules/p-try/index.js +9 -0
- package/node_modules/p-try/license +9 -0
- package/node_modules/p-try/package.json +42 -0
- package/node_modules/p-try/readme.md +58 -0
- package/node_modules/path-exists/index.d.ts +28 -0
- package/node_modules/path-exists/index.js +23 -0
- package/node_modules/path-exists/license +9 -0
- package/node_modules/path-exists/package.json +39 -0
- package/node_modules/path-exists/readme.md +52 -0
- package/node_modules/pngjs/.eslintignore +2 -0
- package/node_modules/pngjs/.eslintrc.json +19 -0
- package/node_modules/pngjs/.prettierignore +3 -0
- package/node_modules/pngjs/LICENSE +20 -0
- package/node_modules/pngjs/README.md +429 -0
- package/node_modules/pngjs/browser.js +16096 -0
- package/node_modules/pngjs/coverage/lcov-report/base.css +354 -0
- package/node_modules/pngjs/coverage/lcov-report/bitmapper.js.html +881 -0
- package/node_modules/pngjs/coverage/lcov-report/bitpacker.js.html +554 -0
- package/node_modules/pngjs/coverage/lcov-report/block-navigation.js +77 -0
- package/node_modules/pngjs/coverage/lcov-report/chunkstream.js.html +647 -0
- package/node_modules/pngjs/coverage/lcov-report/constants.js.html +176 -0
- package/node_modules/pngjs/coverage/lcov-report/crc.js.html +200 -0
- package/node_modules/pngjs/coverage/lcov-report/favicon.png +0 -0
- package/node_modules/pngjs/coverage/lcov-report/filter-pack.js.html +593 -0
- package/node_modules/pngjs/coverage/lcov-report/filter-parse-async.js.html +152 -0
- package/node_modules/pngjs/coverage/lcov-report/filter-parse-sync.js.html +143 -0
- package/node_modules/pngjs/coverage/lcov-report/filter-parse.js.html +611 -0
- package/node_modules/pngjs/coverage/lcov-report/format-normaliser.js.html +359 -0
- package/node_modules/pngjs/coverage/lcov-report/index.html +426 -0
- package/node_modules/pngjs/coverage/lcov-report/interlace.js.html +365 -0
- package/node_modules/pngjs/coverage/lcov-report/packer-async.js.html +230 -0
- package/node_modules/pngjs/coverage/lcov-report/packer-sync.js.html +248 -0
- package/node_modules/pngjs/coverage/lcov-report/packer.js.html +467 -0
- package/node_modules/pngjs/coverage/lcov-report/paeth-predictor.js.html +128 -0
- package/node_modules/pngjs/coverage/lcov-report/parser-async.js.html +575 -0
- package/node_modules/pngjs/coverage/lcov-report/parser-sync.js.html +404 -0
- package/node_modules/pngjs/coverage/lcov-report/parser.js.html +950 -0
- package/node_modules/pngjs/coverage/lcov-report/png-sync.js.html +116 -0
- package/node_modules/pngjs/coverage/lcov-report/png.js.html +662 -0
- package/node_modules/pngjs/coverage/lcov-report/prettify.css +101 -0
- package/node_modules/pngjs/coverage/lcov-report/prettify.js +1007 -0
- package/node_modules/pngjs/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/node_modules/pngjs/coverage/lcov-report/sorter.js +169 -0
- package/node_modules/pngjs/coverage/lcov-report/sync-inflate.js.html +584 -0
- package/node_modules/pngjs/coverage/lcov-report/sync-reader.js.html +215 -0
- package/node_modules/pngjs/coverage/lcov.info +2177 -0
- package/node_modules/pngjs/lib/bitmapper.js +267 -0
- package/node_modules/pngjs/lib/bitpacker.js +158 -0
- package/node_modules/pngjs/lib/chunkstream.js +189 -0
- package/node_modules/pngjs/lib/constants.js +32 -0
- package/node_modules/pngjs/lib/crc.js +40 -0
- package/node_modules/pngjs/lib/filter-pack.js +171 -0
- package/node_modules/pngjs/lib/filter-parse-async.js +24 -0
- package/node_modules/pngjs/lib/filter-parse-sync.js +21 -0
- package/node_modules/pngjs/lib/filter-parse.js +177 -0
- package/node_modules/pngjs/lib/format-normaliser.js +93 -0
- package/node_modules/pngjs/lib/interlace.js +95 -0
- package/node_modules/pngjs/lib/packer-async.js +50 -0
- package/node_modules/pngjs/lib/packer-sync.js +56 -0
- package/node_modules/pngjs/lib/packer.js +129 -0
- package/node_modules/pngjs/lib/paeth-predictor.js +16 -0
- package/node_modules/pngjs/lib/parser-async.js +165 -0
- package/node_modules/pngjs/lib/parser-sync.js +108 -0
- package/node_modules/pngjs/lib/parser.js +290 -0
- package/node_modules/pngjs/lib/png-sync.js +12 -0
- package/node_modules/pngjs/lib/png.js +194 -0
- package/node_modules/pngjs/lib/sync-inflate.js +168 -0
- package/node_modules/pngjs/lib/sync-reader.js +45 -0
- package/node_modules/pngjs/package.json +73 -0
- package/node_modules/qrcode/README.md +770 -0
- package/node_modules/qrcode/bin/qrcode +159 -0
- package/node_modules/qrcode/helper/to-sjis-browser.js +2 -0
- package/node_modules/qrcode/helper/to-sjis.js +105 -0
- package/node_modules/qrcode/lib/browser.js +76 -0
- package/node_modules/qrcode/lib/can-promise.js +7 -0
- package/node_modules/qrcode/lib/core/alignment-pattern.js +83 -0
- package/node_modules/qrcode/lib/core/alphanumeric-data.js +59 -0
- package/node_modules/qrcode/lib/core/bit-buffer.js +37 -0
- package/node_modules/qrcode/lib/core/bit-matrix.js +65 -0
- package/node_modules/qrcode/lib/core/byte-data.js +30 -0
- package/node_modules/qrcode/lib/core/error-correction-code.js +135 -0
- package/node_modules/qrcode/lib/core/error-correction-level.js +50 -0
- package/node_modules/qrcode/lib/core/finder-pattern.js +22 -0
- package/node_modules/qrcode/lib/core/format-info.js +29 -0
- package/node_modules/qrcode/lib/core/galois-field.js +69 -0
- package/node_modules/qrcode/lib/core/kanji-data.js +54 -0
- package/node_modules/qrcode/lib/core/mask-pattern.js +234 -0
- package/node_modules/qrcode/lib/core/mode.js +167 -0
- package/node_modules/qrcode/lib/core/numeric-data.js +43 -0
- package/node_modules/qrcode/lib/core/polynomial.js +62 -0
- package/node_modules/qrcode/lib/core/qrcode.js +495 -0
- package/node_modules/qrcode/lib/core/reed-solomon-encoder.js +56 -0
- package/node_modules/qrcode/lib/core/regex.js +31 -0
- package/node_modules/qrcode/lib/core/segments.js +330 -0
- package/node_modules/qrcode/lib/core/utils.js +63 -0
- package/node_modules/qrcode/lib/core/version-check.js +9 -0
- package/node_modules/qrcode/lib/core/version.js +163 -0
- package/node_modules/qrcode/lib/index.js +12 -0
- package/node_modules/qrcode/lib/renderer/canvas.js +63 -0
- package/node_modules/qrcode/lib/renderer/png.js +78 -0
- package/node_modules/qrcode/lib/renderer/svg-tag.js +81 -0
- package/node_modules/qrcode/lib/renderer/svg.js +19 -0
- package/node_modules/qrcode/lib/renderer/terminal/terminal-small.js +85 -0
- package/node_modules/qrcode/lib/renderer/terminal/terminal.js +49 -0
- package/node_modules/qrcode/lib/renderer/terminal.js +9 -0
- package/node_modules/qrcode/lib/renderer/utf8.js +71 -0
- package/node_modules/qrcode/lib/renderer/utils.js +99 -0
- package/node_modules/qrcode/lib/server.js +138 -0
- package/node_modules/qrcode/license +10 -0
- package/node_modules/qrcode/package.json +77 -0
- package/node_modules/require-directory/.jshintrc +67 -0
- package/node_modules/require-directory/.npmignore +1 -0
- package/node_modules/require-directory/.travis.yml +3 -0
- package/node_modules/require-directory/LICENSE +22 -0
- package/node_modules/require-directory/README.markdown +184 -0
- package/node_modules/require-directory/index.js +86 -0
- package/node_modules/require-directory/package.json +40 -0
- package/node_modules/require-main-filename/CHANGELOG.md +26 -0
- package/node_modules/require-main-filename/LICENSE.txt +14 -0
- package/node_modules/require-main-filename/README.md +26 -0
- package/node_modules/require-main-filename/index.js +18 -0
- package/node_modules/require-main-filename/package.json +35 -0
- package/node_modules/set-blocking/CHANGELOG.md +26 -0
- package/node_modules/set-blocking/LICENSE.txt +14 -0
- package/node_modules/set-blocking/README.md +31 -0
- package/node_modules/set-blocking/index.js +7 -0
- package/node_modules/set-blocking/package.json +42 -0
- package/node_modules/string-width/index.d.ts +29 -0
- package/node_modules/string-width/index.js +47 -0
- package/node_modules/string-width/license +9 -0
- package/node_modules/string-width/package.json +56 -0
- package/node_modules/string-width/readme.md +50 -0
- package/node_modules/strip-ansi/index.d.ts +17 -0
- package/node_modules/strip-ansi/index.js +4 -0
- package/node_modules/strip-ansi/license +9 -0
- package/node_modules/strip-ansi/package.json +54 -0
- package/node_modules/strip-ansi/readme.md +46 -0
- package/node_modules/which-module/LICENSE +13 -0
- package/node_modules/which-module/README.md +58 -0
- package/node_modules/which-module/index.js +9 -0
- package/node_modules/which-module/package.json +41 -0
- package/node_modules/wrap-ansi/index.js +186 -0
- package/node_modules/wrap-ansi/license +9 -0
- package/node_modules/wrap-ansi/package.json +61 -0
- package/node_modules/wrap-ansi/readme.md +97 -0
- package/node_modules/y18n/CHANGELOG.md +35 -0
- package/node_modules/y18n/LICENSE +13 -0
- package/node_modules/y18n/README.md +109 -0
- package/node_modules/y18n/index.js +188 -0
- package/node_modules/y18n/package.json +39 -0
- package/node_modules/yargs/CHANGELOG.md +420 -0
- package/node_modules/yargs/LICENSE +21 -0
- package/node_modules/yargs/README.md +140 -0
- package/node_modules/yargs/build/lib/apply-extends.d.ts +2 -0
- package/node_modules/yargs/build/lib/apply-extends.js +65 -0
- package/node_modules/yargs/build/lib/argsert.d.ts +2 -0
- package/node_modules/yargs/build/lib/argsert.js +65 -0
- package/node_modules/yargs/build/lib/command.d.ts +64 -0
- package/node_modules/yargs/build/lib/command.js +416 -0
- package/node_modules/yargs/build/lib/common-types.d.ts +36 -0
- package/node_modules/yargs/build/lib/common-types.js +25 -0
- package/node_modules/yargs/build/lib/completion-templates.d.ts +2 -0
- package/node_modules/yargs/build/lib/completion-templates.js +50 -0
- package/node_modules/yargs/build/lib/completion.d.ts +21 -0
- package/node_modules/yargs/build/lib/completion.js +135 -0
- package/node_modules/yargs/build/lib/is-promise.d.ts +1 -0
- package/node_modules/yargs/build/lib/is-promise.js +9 -0
- package/node_modules/yargs/build/lib/levenshtein.d.ts +1 -0
- package/node_modules/yargs/build/lib/levenshtein.js +58 -0
- package/node_modules/yargs/build/lib/middleware.d.ts +10 -0
- package/node_modules/yargs/build/lib/middleware.js +57 -0
- package/node_modules/yargs/build/lib/obj-filter.d.ts +1 -0
- package/node_modules/yargs/build/lib/obj-filter.js +14 -0
- package/node_modules/yargs/build/lib/parse-command.d.ts +11 -0
- package/node_modules/yargs/build/lib/parse-command.js +36 -0
- package/node_modules/yargs/build/lib/process-argv.d.ts +2 -0
- package/node_modules/yargs/build/lib/process-argv.js +31 -0
- package/node_modules/yargs/build/lib/usage.d.ts +49 -0
- package/node_modules/yargs/build/lib/usage.js +540 -0
- package/node_modules/yargs/build/lib/validation.d.ts +34 -0
- package/node_modules/yargs/build/lib/validation.js +330 -0
- package/node_modules/yargs/build/lib/yargs.d.ts +274 -0
- package/node_modules/yargs/build/lib/yargs.js +1190 -0
- package/node_modules/yargs/build/lib/yerror.d.ts +4 -0
- package/node_modules/yargs/build/lib/yerror.js +11 -0
- package/node_modules/yargs/index.js +40 -0
- package/node_modules/yargs/locales/be.json +46 -0
- package/node_modules/yargs/locales/de.json +46 -0
- package/node_modules/yargs/locales/en.json +51 -0
- package/node_modules/yargs/locales/es.json +46 -0
- package/node_modules/yargs/locales/fi.json +49 -0
- package/node_modules/yargs/locales/fr.json +53 -0
- package/node_modules/yargs/locales/hi.json +49 -0
- package/node_modules/yargs/locales/hu.json +46 -0
- package/node_modules/yargs/locales/id.json +50 -0
- package/node_modules/yargs/locales/it.json +46 -0
- package/node_modules/yargs/locales/ja.json +51 -0
- package/node_modules/yargs/locales/ko.json +49 -0
- package/node_modules/yargs/locales/nb.json +44 -0
- package/node_modules/yargs/locales/nl.json +49 -0
- package/node_modules/yargs/locales/nn.json +44 -0
- package/node_modules/yargs/locales/pirate.json +13 -0
- package/node_modules/yargs/locales/pl.json +49 -0
- package/node_modules/yargs/locales/pt.json +45 -0
- package/node_modules/yargs/locales/pt_BR.json +48 -0
- package/node_modules/yargs/locales/ru.json +46 -0
- package/node_modules/yargs/locales/th.json +46 -0
- package/node_modules/yargs/locales/tr.json +48 -0
- package/node_modules/yargs/locales/zh_CN.json +48 -0
- package/node_modules/yargs/locales/zh_TW.json +47 -0
- package/node_modules/yargs/package.json +92 -0
- package/node_modules/yargs/yargs.js +14 -0
- package/node_modules/yargs-parser/CHANGELOG.md +601 -0
- package/node_modules/yargs-parser/LICENSE.txt +14 -0
- package/node_modules/yargs-parser/README.md +449 -0
- package/node_modules/yargs-parser/index.js +1032 -0
- package/node_modules/yargs-parser/lib/tokenize-arg-string.js +40 -0
- package/node_modules/yargs-parser/package.json +46 -0
- package/package.json +77 -4
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 插件自更新的**外部监督器**(ADR 0019;评审 #32 note 1286 的 R1,二次复核的 F1/F2/F3/F4)。
|
|
3
|
+
//
|
|
4
|
+
// 为什么必须独立:回滚绝不能依赖新版插件能否加载。本脚本由**旧版插件**启动,
|
|
5
|
+
// 启动前先把自身代码**冻结**到不可变目录(pnpm 可能清理原模块目录),并在 systemd 托管时
|
|
6
|
+
// 用 transient unit **逃逸出宿主 cgroup**(否则停宿主时监督器会被一起杀掉)。
|
|
7
|
+
//
|
|
8
|
+
// 工作方式:
|
|
9
|
+
// 1. `--arm`:写就绪回执 → 接管事务锁 → 等事务进入 `restart-pending`(或终态/超时);
|
|
10
|
+
// 2. 进入后**先安排第一次重启**(旧宿主不会自己加载新代码),再等新实例健康;
|
|
11
|
+
// 3. 健康判据是**正在运行的内容标识**(`plugin.contentSha256`),不是磁盘字节;
|
|
12
|
+
// 4. 不健康 → 用**快照**恢复旧依赖并重装 → 再重启 → 确认旧内容回来;
|
|
13
|
+
// 5. 全程只在终态释放锁,并把结果原子落盘供手机查询。
|
|
14
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { dirname, join } from "node:path";
|
|
16
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
17
|
+
import {
|
|
18
|
+
TRANSACTION_PHASE,
|
|
19
|
+
acquireLock,
|
|
20
|
+
assertUpdateLease,
|
|
21
|
+
restoreOwnedProfile,
|
|
22
|
+
publishOwnedSnapshotLink,
|
|
23
|
+
finishOwnedTerminal,
|
|
24
|
+
claimOwnedRecovery,
|
|
25
|
+
defaultIsPidAlive,
|
|
26
|
+
writeOwnedTransaction,
|
|
27
|
+
switchOwnedDependency,
|
|
28
|
+
handOverLock,
|
|
29
|
+
readTransaction,
|
|
30
|
+
releaseLock,
|
|
31
|
+
snapshotReady,
|
|
32
|
+
updateLockPath,
|
|
33
|
+
writeTransaction,
|
|
34
|
+
} from "./plugin-update-transaction.js";
|
|
35
|
+
import { pluginContentDigest } from "./plugin-content-digest.js";
|
|
36
|
+
import { verifyFrozenPackage } from "./runtime-dependency-snapshot.js";
|
|
37
|
+
import { restartPlan } from "./plugin-self-update.js";
|
|
38
|
+
|
|
39
|
+
/** 等待宿主的默认超时:足够冷启动(含语料预热),又不至于让用户无限等。 */
|
|
40
|
+
export const DEFAULT_WAIT_TIMEOUT_MS = 180_000;
|
|
41
|
+
export const DEFAULT_POLL_MS = 2_000;
|
|
42
|
+
/** 等事务进入 restart-pending 的上限(下载 + 安装可能很慢)。 */
|
|
43
|
+
export const DEFAULT_ARM_TIMEOUT_MS = 15 * 60 * 1000;
|
|
44
|
+
|
|
45
|
+
/** 解析命令行参数。 */
|
|
46
|
+
export function parseArgs(argv) {
|
|
47
|
+
const out = {};
|
|
48
|
+
for (let i = 0; i < argv.length; i++) {
|
|
49
|
+
const key = argv[i];
|
|
50
|
+
if (!key.startsWith("--")) continue;
|
|
51
|
+
const next = argv[i + 1];
|
|
52
|
+
if (next === undefined || next.startsWith("--")) out[key.slice(2)] = "true";
|
|
53
|
+
else {
|
|
54
|
+
out[key.slice(2)] = next;
|
|
55
|
+
i++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 轮询本机宿主,直到它报告出**期望的内容标识**(或期望版本)。 */
|
|
62
|
+
export async function waitForHost({
|
|
63
|
+
port,
|
|
64
|
+
basePath = "/m",
|
|
65
|
+
token = "",
|
|
66
|
+
expectContentSha256 = "",
|
|
67
|
+
expectVersion = "",
|
|
68
|
+
timeoutMs = DEFAULT_WAIT_TIMEOUT_MS,
|
|
69
|
+
pollMs = DEFAULT_POLL_MS,
|
|
70
|
+
fetchImpl = fetch,
|
|
71
|
+
now = () => Date.now(),
|
|
72
|
+
sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
73
|
+
} = {}) {
|
|
74
|
+
const deadline = now() + timeoutMs;
|
|
75
|
+
const url = `http://127.0.0.1:${port}${basePath}/api/bootstrap`;
|
|
76
|
+
let lastError = "no-response";
|
|
77
|
+
while (now() < deadline) {
|
|
78
|
+
try {
|
|
79
|
+
const res = await fetchImpl(url, {
|
|
80
|
+
headers: token ? { "x-mobile-token": token } : {},
|
|
81
|
+
signal: AbortSignal.timeout(Math.max(1000, pollMs)),
|
|
82
|
+
});
|
|
83
|
+
if (res?.ok) {
|
|
84
|
+
const body = await res.json();
|
|
85
|
+
const plugin = body?.plugin ?? {};
|
|
86
|
+
const contentOk = expectContentSha256 !== "" && plugin.contentSha256 === expectContentSha256;
|
|
87
|
+
const versionOk = expectContentSha256 === "" && expectVersion !== "" && plugin.version === expectVersion;
|
|
88
|
+
const anyOk = expectContentSha256 === "" && expectVersion === "" && Boolean(body?.ok);
|
|
89
|
+
if (contentOk || versionOk || anyOk) {
|
|
90
|
+
return { ok: true, plugin, matched: contentOk ? "content" : versionOk ? "version" : "any" };
|
|
91
|
+
}
|
|
92
|
+
lastError = `content-mismatch:${plugin.contentSha256 ?? "none"}`;
|
|
93
|
+
} else {
|
|
94
|
+
lastError = `http-${res?.status ?? "?"}`;
|
|
95
|
+
}
|
|
96
|
+
} catch (e) {
|
|
97
|
+
lastError = String(e?.message ?? e);
|
|
98
|
+
}
|
|
99
|
+
await sleep(pollMs);
|
|
100
|
+
}
|
|
101
|
+
return { ok: false, error: `host-wait-timeout: ${lastError}` };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 找出**当前**占用该端口的进程(F3:回滚不能杀更新前的旧 PID)。 */
|
|
105
|
+
export function findPortOwnerPid(port, { run = spawnSync } = {}) {
|
|
106
|
+
if (!Number.isFinite(port) || port <= 0) return null;
|
|
107
|
+
try {
|
|
108
|
+
const ss = run("ss", ["-ltnpH", `sport = :${port}`], { encoding: "utf8" });
|
|
109
|
+
const m = /pid=(\d+)/.exec(ss?.stdout ?? "");
|
|
110
|
+
if (m) return Number(m[1]);
|
|
111
|
+
} catch {
|
|
112
|
+
// 继续尝试 lsof
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const lsof = run("lsof", ["-ti", `tcp:${port}`, "-sTCP:LISTEN"], { encoding: "utf8" });
|
|
116
|
+
const first = (lsof?.stdout ?? "").trim().split("\n")[0];
|
|
117
|
+
if (/^\d+$/.test(first)) return Number(first);
|
|
118
|
+
} catch {
|
|
119
|
+
// 没有可用工具
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 生成"杀掉当前端口占用者"的 shell 片段(找不到就退回给定 pid)。 */
|
|
125
|
+
export function portKillSnippet(port, fallbackPid = null) {
|
|
126
|
+
const lines = [
|
|
127
|
+
`P=$(ss -ltnpH "sport = :${port}" 2>/dev/null | grep -oP 'pid=\\K[0-9]+' | head -1)`,
|
|
128
|
+
`[ -z "$P" ] && P=$(lsof -ti tcp:${port} -sTCP:LISTEN 2>/dev/null | head -1)`,
|
|
129
|
+
];
|
|
130
|
+
if (fallbackPid) lines.push(`[ -z "$P" ] && P=${fallbackPid}`);
|
|
131
|
+
lines.push('[ -n "$P" ] && kill -TERM "$P" 2>/dev/null');
|
|
132
|
+
return lines.join("; ");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 默认重启器:**按当前实例**(端口占用者)终止,而不是事务里记的旧 PID;
|
|
137
|
+
* 前台形态自行重新拉起。**返回结果不再被忽略**(F3)。
|
|
138
|
+
*/
|
|
139
|
+
export function defaultRestart({ restartForm, port, pid = null, profile = "web", dshCommand = "dsh", log = () => {} }) {
|
|
140
|
+
const plan = restartPlan({ form: restartForm, pid: null, profile, dshCommand });
|
|
141
|
+
if (plan.mode === "unsupported") return { ok: false, error: "restart-unsupported" };
|
|
142
|
+
const kill = portKillSnippet(port, pid);
|
|
143
|
+
const script = plan.mode === "respawn"
|
|
144
|
+
? `sleep 1; ${kill}; sleep 2; nohup ${dshCommand} web --profile ${profile} >/dev/null 2>&1 &`
|
|
145
|
+
: `sleep 1; ${kill}`;
|
|
146
|
+
const r = spawnSync("bash", ["-lc", script], { encoding: "utf8" });
|
|
147
|
+
if (r.error) return { ok: false, error: r.error.message };
|
|
148
|
+
if (r.status !== 0) return { ok: false, error: `restart-script-exit-${r.status}: ${(r.stderr || "").trim()}` };
|
|
149
|
+
log(`supervisor: 已安排重启(${plan.mode},目标=当前端口占用者)`);
|
|
150
|
+
return { ok: true, script };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 默认恢复器(F4):把依赖指回**快照**(而不是可能已变的原 clone/tag/source),
|
|
155
|
+
* 再重装;并检查安装结果。
|
|
156
|
+
*/
|
|
157
|
+
export async function defaultRestore({ profileDir, snapshotDir: snapDir, expectedContentSha256, profileSnapshot, lockPath, lease }) {
|
|
158
|
+
if (!snapDir || !snapshotReady(snapDir) || (expectedContentSha256 && pluginContentDigest(snapDir) !== expectedContentSha256)) {
|
|
159
|
+
return { ok: false, error: "snapshot-unavailable" };
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
assertUpdateLease(lockPath, lease);
|
|
163
|
+
// 一次受代际保护的仲裁里恢复 profile/lockfile(A 阻塞:不再有 assert→write 空窗)。
|
|
164
|
+
const restored = restoreOwnedProfile({ profileDir, snapshotDir: snapDir, snapshot: profileSnapshot, lockPath, lease });
|
|
165
|
+
if (!restored.ok) return { ok: false, error: restored.error };
|
|
166
|
+
// 直接把**已验证自包含**的快照发布为安装目录:旧字节精确、离线可加载,
|
|
167
|
+
// 不依赖 pnpm 把依赖内联进插件目录(真实 pnpm 会放到 sibling,containment 判据必错)。
|
|
168
|
+
const published = publishOwnedSnapshotLink({ profileDir, snapshotDir: snapDir, lockPath, lease });
|
|
169
|
+
if (!published.ok) return { ok: false, error: published.error };
|
|
170
|
+
assertUpdateLease(lockPath, lease);
|
|
171
|
+
const actualRoot = join(profileDir, "node_modules", "dsh-mobile-remote");
|
|
172
|
+
if (pluginContentDigest(actualRoot) !== expectedContentSha256) return { ok: false, error: "restored-content-mismatch" };
|
|
173
|
+
const verified = verifyFrozenPackage(actualRoot);
|
|
174
|
+
if (!verified.ok) return { ok: false, error: `restored-snapshot-invalid: ${verified.error}` };
|
|
175
|
+
return { ok: true, restoredTo: snapDir, digest: verified.digest };
|
|
176
|
+
} catch(e) { return { ok: false, error: e.message }; }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 默认安装器(监督器是独立进程,阻塞没关系)。 */
|
|
180
|
+
export function defaultRunInstall(profileDir) {
|
|
181
|
+
const r = spawnSync("pnpm", ["install"], { cwd: profileDir, encoding: "utf8" });
|
|
182
|
+
if (r.error) return { ok: false, error: r.error.message };
|
|
183
|
+
if (r.status !== 0) return { ok: false, error: (r.stderr || `exit ${r.status}`).trim() };
|
|
184
|
+
return { ok: true };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 推进一次已进入 `restart-pending` 的事务。
|
|
189
|
+
* **顺序**:先安排第一次重启(F1),再等新实例健康;不健康才走回滚。
|
|
190
|
+
*/
|
|
191
|
+
export async function runTransaction({ stateFile, tx, timeoutMs = DEFAULT_WAIT_TIMEOUT_MS,
|
|
192
|
+
pollMs = DEFAULT_POLL_MS, maxRollbacks = 1, waitForHost: waitImpl = waitForHost,
|
|
193
|
+
restore = defaultRestore, restart = defaultRestart, runInstall = defaultRunInstall, log = () => {}, now = () => Date.now() } = {}) {
|
|
194
|
+
const lockPath = tx.lockPath || join(dirname(stateFile), "plugin-update.lock.d");
|
|
195
|
+
const lease = tx.lease;
|
|
196
|
+
const phaseAt = () => tx.phase;
|
|
197
|
+
const save = (patch, expectedPhases) => { tx = writeOwnedTransaction(stateFile, { ...tx, ...patch }, { lockPath, lease, currentFile: tx.currentFile, expectedPhases }); return tx; };
|
|
198
|
+
// 终态与释放合一:不存在"已终态但锁还在/已被释放"的空窗(C3)。
|
|
199
|
+
const finish = (phase, result, expectedPhases) => finishOwnedTerminal(stateFile, { phase, result, expectedPhases }, { lockPath, lease, currentFile: tx.currentFile });
|
|
200
|
+
try {
|
|
201
|
+
assertUpdateLease(lockPath, lease);
|
|
202
|
+
let failure = tx.recoveryCause || tx.result?.reason;
|
|
203
|
+
// 已经处于 rolling-back(安装失败/恢复重试):**直接续做恢复**,不重复"先重启再等待"。
|
|
204
|
+
if (tx.phase !== TRANSACTION_PHASE.ROLLING_BACK) {
|
|
205
|
+
const entryPhase = phaseAt();
|
|
206
|
+
const result = await restart({ ...tx, pid: tx.hostPid, log });
|
|
207
|
+
assertUpdateLease(lockPath, lease);
|
|
208
|
+
if (!result?.ok) failure = `restart-failed: ${result?.error ?? "unknown"}`;
|
|
209
|
+
else {
|
|
210
|
+
const healthy = await waitImpl({ ...tx, expectContentSha256: tx.targetContentSha256,
|
|
211
|
+
expectVersion: tx.targetVersion, timeoutMs, pollMs });
|
|
212
|
+
assertUpdateLease(lockPath, lease);
|
|
213
|
+
if (healthy.ok) return finish(TRANSACTION_PHASE.SETTLED, { ok: true, matched: healthy.matched, at: new Date(now()).toISOString() }, [entryPhase]);
|
|
214
|
+
failure = healthy.error;
|
|
215
|
+
}
|
|
216
|
+
if (!tx.previousSnapshotDir) return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "rollback-target-missing", error: failure }, [entryPhase]);
|
|
217
|
+
if ((tx.attempts ?? 0) >= maxRollbacks) return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "rollback-exhausted", error: failure }, [entryPhase]);
|
|
218
|
+
// 正常失败与"续做恢复"共用同一入口;CAS 用**进入时的真实阶段**,不再硬编码。
|
|
219
|
+
save({ phase: TRANSACTION_PHASE.ROLLING_BACK, attempts: (tx.attempts ?? 0) + 1, rolledBack: true,
|
|
220
|
+
result: { ok: false, reason: "rolling-back", error: failure } }, [entryPhase]);
|
|
221
|
+
}
|
|
222
|
+
assertUpdateLease(lockPath, lease);
|
|
223
|
+
if (!tx.previousSnapshotDir) return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "rollback-target-missing", error: failure }, ["rolling-back"]);
|
|
224
|
+
if ((tx.attempts ?? 0) > maxRollbacks) return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "rollback-exhausted", error: failure }, ["rolling-back"]);
|
|
225
|
+
// 同代唯一 claim:两个监督器同时重试时,只有一方真的执行恢复与重启。
|
|
226
|
+
const owner = `${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
227
|
+
const claimed = claimOwnedRecovery(stateFile, { owner }, { lockPath, lease, currentFile: tx.currentFile });
|
|
228
|
+
if (!claimed.ok) return { ok: false, error: claimed.error, transaction: claimed.transaction };
|
|
229
|
+
tx = claimed.transaction;
|
|
230
|
+
const restored = await restore({ profileDir: tx.profileDir, snapshotDir: tx.previousSnapshotDir,
|
|
231
|
+
expectedContentSha256: tx.previousContentSha256, profileSnapshot: tx.profileSnapshot, lockPath, lease, runInstall });
|
|
232
|
+
assertUpdateLease(lockPath, lease);
|
|
233
|
+
if (!restored?.ok) return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "rollback-restore-failed", error: restored?.error }, ["rolling-back"]);
|
|
234
|
+
const result = await restart({ ...tx, pid: null, log });
|
|
235
|
+
assertUpdateLease(lockPath, lease);
|
|
236
|
+
if (!result?.ok) return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "rollback-restart-failed", error: result?.error }, ["rolling-back"]);
|
|
237
|
+
const back = await waitImpl({ ...tx, expectContentSha256: tx.previousContentSha256,
|
|
238
|
+
expectVersion: "", timeoutMs, pollMs });
|
|
239
|
+
assertUpdateLease(lockPath, lease);
|
|
240
|
+
return finish(back.ok ? TRANSACTION_PHASE.ROLLED_BACK : TRANSACTION_PHASE.FAILED,
|
|
241
|
+
{ ok: false, reason: back.ok ? "rolled-back" : "rollback-unconfirmed", error: back.ok ? failure : back.error }, ["rolling-back"]);
|
|
242
|
+
} catch(e) {
|
|
243
|
+
if (e.message === "lease-lost") return { ok: false, error: "lease-lost" };
|
|
244
|
+
try { return finish(TRANSACTION_PHASE.FAILED, { ok: false, reason: "supervisor-error", error: e.message }, [tx.phase]); }
|
|
245
|
+
catch { return { ok: false, error: e.message }; }
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export async function superviseUpdate(options = {}) {
|
|
250
|
+
const tx = readTransaction(options.stateFile);
|
|
251
|
+
if (!tx) return { ok: false, error: "no-transaction" };
|
|
252
|
+
if (options.opId && (options.opId !== tx.opId || options.generation !== tx.lease?.generation)) return { ok: false, error: "operation-mismatch" };
|
|
253
|
+
if (!["restart-pending", "rolling-back"].includes(tx.phase)) return { ok: false, error: `not-restart-pending:${tx.phase}` };
|
|
254
|
+
return runTransaction({ ...options, tx });
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export async function superviseArmed({ stateFile, readyFile, opId, generation,
|
|
258
|
+
armTimeoutMs = DEFAULT_ARM_TIMEOUT_MS, pollMs = DEFAULT_POLL_MS,
|
|
259
|
+
log = () => {}, sleep = ms => new Promise(r => setTimeout(r, ms)), now = () => Date.now(), ...rest } = {}) {
|
|
260
|
+
const tx0 = readTransaction(stateFile);
|
|
261
|
+
if (!tx0 || tx0.opId !== opId || tx0.lease?.generation !== generation) return { ok: false, error: "operation-mismatch" };
|
|
262
|
+
const lockPath = tx0.lockPath, lease = tx0.lease;
|
|
263
|
+
if (!handOverLock(lockPath, { lease, toPid: process.pid })) return { ok: false, error: "lease-lost" };
|
|
264
|
+
let cgroup = "";
|
|
265
|
+
try { cgroup = readFileSync("/proc/self/cgroup", "utf8").trim(); } catch { /* portable ready receipt */ }
|
|
266
|
+
mkdirSync(dirname(readyFile), { recursive: true });
|
|
267
|
+
writeFileSync(readyFile, JSON.stringify({ pid: process.pid, opId, generation, cgroup, at: new Date(now()).toISOString() }), { mode: 0o600 });
|
|
268
|
+
const deadline = now() + armTimeoutMs;
|
|
269
|
+
while (now() < deadline) {
|
|
270
|
+
const tx = readTransaction(stateFile);
|
|
271
|
+
if (!tx || tx.opId !== opId || tx.lease?.generation !== generation) return { ok: false, error: "operation-mismatch" };
|
|
272
|
+
try { assertUpdateLease(lockPath, lease); } catch { return { ok: false, error: "lease-lost" }; }
|
|
273
|
+
if (["restart-pending","rolling-back"].includes(tx.phase)) return runTransaction({ stateFile, tx, log, ...rest });
|
|
274
|
+
if (["settled","rolled-back","failed"].includes(tx.phase)) return tx;
|
|
275
|
+
if (!defaultIsPidAlive(tx.hostPid) && tx.phase === "installing") {
|
|
276
|
+
const recovering = writeOwnedTransaction(stateFile, { ...tx, phase: "rolling-back", recoveryCause: "install-worker-gone", rolledBack: true }, { lockPath, lease, currentFile: tx.currentFile });
|
|
277
|
+
return runTransaction({ stateFile, tx: recovering, log, ...rest });
|
|
278
|
+
}
|
|
279
|
+
await sleep(pollMs);
|
|
280
|
+
}
|
|
281
|
+
// Do not race a live installer. Keep its protected recovery metadata, rather than granting a new transaction.
|
|
282
|
+
const current = readTransaction(stateFile);
|
|
283
|
+
if (current.phase === "staging") {
|
|
284
|
+
// 终态 + 释放同一次仲裁;不覆盖已推进到 installing 的事务(phase CAS)。
|
|
285
|
+
try { finishOwnedTerminal(stateFile, { phase: "failed", result: { ok: false, reason: "arm-timeout" }, expectedPhases: ["staging"] }, { lockPath, lease, currentFile: current.currentFile }); }
|
|
286
|
+
catch (e) { log(`supervisor: 保留受保护事务(${e.message})`); }
|
|
287
|
+
}
|
|
288
|
+
return { ok: false, error: "arm-timeout" };
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async function main() {
|
|
292
|
+
const args = parseArgs(process.argv.slice(2));
|
|
293
|
+
const stateFile = args.state;
|
|
294
|
+
if (!stateFile) {
|
|
295
|
+
console.error("usage: node update-supervisor.js --state <transaction.json> [--arm --ready <file>] [--timeout 180]");
|
|
296
|
+
process.exit(2);
|
|
297
|
+
}
|
|
298
|
+
const timeoutMs = Number(args.timeout ?? 0) > 0 ? Number(args.timeout) * 1000 : DEFAULT_WAIT_TIMEOUT_MS;
|
|
299
|
+
const pollMs = Number(args.poll ?? 0) > 0 ? Number(args.poll) : DEFAULT_POLL_MS;
|
|
300
|
+
const log = (m) => console.log(m);
|
|
301
|
+
const finalState = args.arm
|
|
302
|
+
? await superviseArmed({ stateFile, readyFile: args.ready, opId: args.op, generation: args.generation, timeoutMs, pollMs, log })
|
|
303
|
+
: await superviseUpdate({ stateFile, opId: args.op, generation: args.generation, timeoutMs, pollMs, log });
|
|
304
|
+
console.log(`supervisor: 结束于 ${finalState?.phase ?? "unknown"}`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (process.argv[1] && process.argv[1].endsWith("update-supervisor.js")) {
|
|
308
|
+
main().catch((e) => {
|
|
309
|
+
console.error(`supervisor: 异常 ${e?.stack ?? e}`);
|
|
310
|
+
process.exit(1);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare namespace ansiRegex {
|
|
2
|
+
interface Options {
|
|
3
|
+
/**
|
|
4
|
+
Match only the first ANSI escape.
|
|
5
|
+
|
|
6
|
+
@default false
|
|
7
|
+
*/
|
|
8
|
+
onlyFirst: boolean;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
Regular expression for matching ANSI escape codes.
|
|
14
|
+
|
|
15
|
+
@example
|
|
16
|
+
```
|
|
17
|
+
import ansiRegex = require('ansi-regex');
|
|
18
|
+
|
|
19
|
+
ansiRegex().test('\u001B[4mcake\u001B[0m');
|
|
20
|
+
//=> true
|
|
21
|
+
|
|
22
|
+
ansiRegex().test('cake');
|
|
23
|
+
//=> false
|
|
24
|
+
|
|
25
|
+
'\u001B[4mcake\u001B[0m'.match(ansiRegex());
|
|
26
|
+
//=> ['\u001B[4m', '\u001B[0m']
|
|
27
|
+
|
|
28
|
+
'\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
|
|
29
|
+
//=> ['\u001B[4m']
|
|
30
|
+
|
|
31
|
+
'\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
|
|
32
|
+
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
|
|
33
|
+
```
|
|
34
|
+
*/
|
|
35
|
+
declare function ansiRegex(options?: ansiRegex.Options): RegExp;
|
|
36
|
+
|
|
37
|
+
export = ansiRegex;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = ({onlyFirst = false} = {}) => {
|
|
4
|
+
const pattern = [
|
|
5
|
+
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
|
|
6
|
+
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
|
|
7
|
+
].join('|');
|
|
8
|
+
|
|
9
|
+
return new RegExp(pattern, onlyFirst ? undefined : 'g');
|
|
10
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ansi-regex",
|
|
3
|
+
"version": "5.0.1",
|
|
4
|
+
"description": "Regular expression for matching ANSI escape codes",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "chalk/ansi-regex",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Sindre Sorhus",
|
|
9
|
+
"email": "sindresorhus@gmail.com",
|
|
10
|
+
"url": "sindresorhus.com"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=8"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "xo && ava && tsd",
|
|
17
|
+
"view-supported": "node fixtures/view-codes.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"index.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"ansi",
|
|
25
|
+
"styles",
|
|
26
|
+
"color",
|
|
27
|
+
"colour",
|
|
28
|
+
"colors",
|
|
29
|
+
"terminal",
|
|
30
|
+
"console",
|
|
31
|
+
"cli",
|
|
32
|
+
"string",
|
|
33
|
+
"tty",
|
|
34
|
+
"escape",
|
|
35
|
+
"formatting",
|
|
36
|
+
"rgb",
|
|
37
|
+
"256",
|
|
38
|
+
"shell",
|
|
39
|
+
"xterm",
|
|
40
|
+
"command-line",
|
|
41
|
+
"text",
|
|
42
|
+
"regex",
|
|
43
|
+
"regexp",
|
|
44
|
+
"re",
|
|
45
|
+
"match",
|
|
46
|
+
"test",
|
|
47
|
+
"find",
|
|
48
|
+
"pattern"
|
|
49
|
+
],
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"ava": "^2.4.0",
|
|
52
|
+
"tsd": "^0.9.0",
|
|
53
|
+
"xo": "^0.25.3"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# ansi-regex
|
|
2
|
+
|
|
3
|
+
> Regular expression for matching [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
$ npm install ansi-regex
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const ansiRegex = require('ansi-regex');
|
|
17
|
+
|
|
18
|
+
ansiRegex().test('\u001B[4mcake\u001B[0m');
|
|
19
|
+
//=> true
|
|
20
|
+
|
|
21
|
+
ansiRegex().test('cake');
|
|
22
|
+
//=> false
|
|
23
|
+
|
|
24
|
+
'\u001B[4mcake\u001B[0m'.match(ansiRegex());
|
|
25
|
+
//=> ['\u001B[4m', '\u001B[0m']
|
|
26
|
+
|
|
27
|
+
'\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
|
|
28
|
+
//=> ['\u001B[4m']
|
|
29
|
+
|
|
30
|
+
'\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
|
|
31
|
+
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
### ansiRegex(options?)
|
|
38
|
+
|
|
39
|
+
Returns a regex for matching ANSI escape codes.
|
|
40
|
+
|
|
41
|
+
#### options
|
|
42
|
+
|
|
43
|
+
Type: `object`
|
|
44
|
+
|
|
45
|
+
##### onlyFirst
|
|
46
|
+
|
|
47
|
+
Type: `boolean`<br>
|
|
48
|
+
Default: `false` *(Matches any ANSI escape codes in a string)*
|
|
49
|
+
|
|
50
|
+
Match only the first ANSI escape.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## FAQ
|
|
54
|
+
|
|
55
|
+
### Why do you test for codes not in the ECMA 48 standard?
|
|
56
|
+
|
|
57
|
+
Some of the codes we run as a test are codes that we acquired finding various lists of non-standard or manufacturer specific codes. We test for both standard and non-standard codes, as most of them follow the same or similar format and can be safely matched in strings without the risk of removing actual string content. There are a few non-standard control codes that do not follow the traditional format (i.e. they end in numbers) thus forcing us to exclude them from the test because we cannot reliably match them.
|
|
58
|
+
|
|
59
|
+
On the historical side, those ECMA standards were established in the early 90's whereas the VT100, for example, was designed in the mid/late 70's. At that point in time, control codes were still pretty ungoverned and engineers used them for a multitude of things, namely to activate hardware ports that may have been proprietary. Somewhere else you see a similar 'anarchy' of codes is in the x86 architecture for processors; there are a ton of "interrupts" that can mean different things on certain brands of processors, most of which have been phased out.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## Maintainers
|
|
63
|
+
|
|
64
|
+
- [Sindre Sorhus](https://github.com/sindresorhus)
|
|
65
|
+
- [Josh Junon](https://github.com/qix-)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
<div align="center">
|
|
71
|
+
<b>
|
|
72
|
+
<a href="https://tidelift.com/subscription/pkg/npm-ansi-regex?utm_source=npm-ansi-regex&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|
73
|
+
</b>
|
|
74
|
+
<br>
|
|
75
|
+
<sub>
|
|
76
|
+
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
77
|
+
</sub>
|
|
78
|
+
</div>
|