npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mtop-devtools/daemon-core

v1.40.0

Published

Shared Node daemon lifecycle primitives for Mtop DevTools — process liveness, JSON record registry, log rotation, and a detached daemon manager

Readme

@mtop-devtools/daemon-core

Mtop DevTools 各 Node 包共享的守护进程生命周期原语:进程存活探测、JSON 记录注册表、日志滚动,以及一个 detached 守护进程管理器。

cloud-servercloud-connectornative-host 里原本各写一份、容易彼此漂移的 daemon 逻辑(isAlive、原子读写注册表、日志滚动、按 key 派生路径)收敛到这一处,确保「写方」和「读方」对记录位置与格式始终一致。

仅供 monorepo 内部使用(Node-only)。cloud-server / cloud-connector 以普通依赖引入(运行时从 npm 解析),native-host 在打包时直接内联。

API

进程存活

isAlive(pid: number | undefined | null): boolean

用 signal-0 探测进程是否存活(EPERM 视为存活——进程存在但属于他人)。

路径派生(多实例)

DAEMON_HOME                                 // ~/.mtop-devtools
hashKey(key: string): string                // sha256(key) 前 16 位十六进制
keyedDir(subdir: string): string            // ~/.mtop-devtools/<subdir>
keyedConfigFile(subdir, key): string        // ~/.mtop-devtools/<subdir>/<hash>.json
keyedLogFile(subdir, key): string           // ~/.mtop-devtools/<subdir>/<hash>.log

多实例子系统(如按 serverUrl 区分的连接器)用这些 helper 派生每实例的文件路径。所有读写方都必须经由它们派生路径,这样彼此才不会对记录位置产生分歧。

记录注册表

interface DaemonRecord { pid: number; startedAt?: string; [k: string]: unknown }

ensureDir(dir: string): void
writeRecord(file: string, record: DaemonRecord): void   // 临时文件 + rename 原子写
readRecord<T>(file: string): T | null
removeRecord(file: string): void
listRecordFiles(dir: string): string[]                  // 目录下所有 *.json 绝对路径
readLiveRecords<T>(dir: string): T[]                    // 读取存活记录,并顺带清理死记录

守护进程管理器

createDaemon(opts: CreateDaemonOptions): Daemon

围绕一对 (configFile, logFile) 构建单实例守护进程管理器。模型:start()--foreground detached 方式重新拉起消费方自己的 CLI;子进程运行实际服务,就绪后把自己的 pid 写入 configFile,父进程轮询 record.pid === child.pid 确认其已起来(而非死亡或卡住)。多实例子系统按实例 key 派生 configFile/logFile,为每个实例各创建一个管理器。

interface CreateDaemonOptions {
  name: string;            // 日志标签,如 'CloudServer' / 'CloudConnector'
  scriptPath: string;      // 可重新拉起的 CLI 脚本绝对路径(消费方打包后的 cli.js)
  configFile: string;      // 本实例的 JSON 记录文件(由前台子进程写入)
  logFile: string;         // 本实例的日志文件
  foregroundArgs?: string[];   // 追加在 `--foreground` 之后的 argv
  readyTimeoutMs?: number;     // 等待子进程写记录的超时(默认 15000ms)
  formatInfo?: (record: DaemonRecord) => string | null;  // 起来后额外打印的信息
}

interface Daemon {
  start(): Promise<void>;
  stop(): Promise<void>;
  restart(): Promise<void>;
  status(): void;
  logs(lines?: number): void;
  running(): DaemonRecord | null;
}

scriptPath 必须是消费方自己的 cli.js:用打包进 cli 入口的某个模块里的 fileURLToPath(import.meta.url) 计算。daemon-core 自身被独立解析时,它的 import.meta.url 指向 daemon-core,而不是 cli。

日志

rotateLogIfNeeded(logFile: string, maxBytes?: number): void  // 超限则滚动为 <log>.1,默认 5MB
tailFile(logFile: string, lines: number): string

使用方

  • cloud-server —— createDaemon(单实例,~/.mtop-devtools/cloud-server.json
  • cloud-connector —— createDaemon(多实例,按 serverUrl 区分,~/.mtop-devtools/connectors/<hash>.json)+ readLiveRecords 列举
  • native-host —— 仅用原语(isAlive / keyedConfigFile / readRecord / readLiveRecords 等)读取连接器共享注册表,以发现/接管/列举连接器