{"id":13457791,"url":"https://github.com/Tencent/TSW","last_synced_at":"2025-03-24T14:32:20.674Z","repository":{"id":32658746,"uuid":"132575923","full_name":"Tencent/TSW","owner":"Tencent","description":"Tencent Server Web","archived":false,"fork":false,"pushed_at":"2024-08-30T13:43:14.000Z","size":8222,"stargazers_count":1803,"open_issues_count":8,"forks_count":186,"subscribers_count":88,"default_branch":"master","last_synced_at":"2025-03-19T01:18:38.424Z","etag":null,"topics":["capture","javascript","js","log","monitor","nodejs","observability","server","tsw","web"],"latest_commit_sha":null,"homepage":"https://tswjs.org/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tencent.png","metadata":{"files":{"readme":"README.md","changelog":"changeLog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-08T08:08:34.000Z","updated_at":"2025-03-15T16:15:00.000Z","dependencies_parsed_at":"2023-01-14T22:00:34.760Z","dependency_job_id":"ee0e687f-6cc5-4ea1-a0b7-73f4e7de57b6","html_url":"https://github.com/Tencent/TSW","commit_stats":null,"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2FTSW","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2FTSW/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2FTSW/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2FTSW/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tencent","download_url":"https://codeload.github.com/Tencent/TSW/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245289655,"owners_count":20591112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["capture","javascript","js","log","monitor","nodejs","observability","server","tsw","web"],"created_at":"2024-07-31T09:00:36.972Z","updated_at":"2025-03-24T14:32:20.105Z","avatar_url":"https://github.com/Tencent.png","language":"TypeScript","readme":"# [Tencent Server Web 2.0](https://tswjs.org)\n\n\n[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Tencent/TSW/blob/master/LICENSE) [![Build Status](https://github.com/tencent/tsw/workflows/build/badge.svg)](https://github.com/Tencent/TSW/actions?query=workflow%3Abuild) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) [![codecov](https://codecov.io/gh/tencent/tsw/branch/master/graph/badge.svg)](https://codecov.io/gh/tencent/tsw)\n\n\u003ch2 align=\"center\"\u003eWhat is it\u003c/h2\u003e\n\nTencent Server Web(TSW) 是一套面向 WEB 前端开发者，以提升问题定位效率为初衷，提供 **染色抓包** 和 **全息日志** 的 Node.js 基础设施。TSW 关注业务的运维监控能力，适用于 http、https 协议的业务场景，可无缝与现有应用（Koa、Express）进行整合。\n\nTSW 2.0 在 1.0 的基础上抽丝剥茧，辅以现代化的设计模式，去除了 1.0 中的大量糟粕，同时对容器化、云原生更加友好。做到了无侵入、低成本接入。\n\n\u003ch2 align=\"center\"\u003eHighlights\u003c/h2\u003e\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003e\u003ch4 align=\"center\"\u003e🚀\u003ch4 align=\"center\"\u003e0 侵入\u003c/h4 align=\"center\"\u003e\u003c/th\u003e\n    \u003cth\u003e\u003ch4 align=\"center\"\u003e📒\u003c/h4 align=\"center\"\u003e\u003ch4 align=\"center\"\u003e全息日志\u003c/h4 align=\"center\"\u003e\u003c/th\u003e\n    \u003cth\u003e\u003ch4 align=\"center\"\u003e🛠\u003c/h4 align=\"center\"\u003e\u003ch4 align=\"center\"\u003e请求抓包\u003c/h4 align=\"center\"\u003e\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd width=\"33%\"\u003e通过 Hack NodeJS 底层代码实现功能。对原有业务代码 0 侵入。\u003c/td\u003e\n    \u003ctd width=\"33%\"\u003e按照请求聚类的显微镜级别的全息日志，给开发者完美的现场还原。\u003c/td\u003e\n    \u003ctd width=\"33%\"\u003e可抓取 Server 端向外部发送的所有请求的完整包体内容，与后台沟通再无障碍。\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003ch2 align=\"center\"\u003e特别提醒\u003c/h2\u003e\n\n### TSW 1.0 迁移到 2.0 需要注意的地方\n\n - TSW 1.0 服务包含进程管理，日志管理等多项内置模块，在进行往 2.0 迁移的时候，请选择其他组件完成替代，如引入 PM2 管理 Node 进程，使用 winston 进行日志管理\n - 另外 2.0 没有包含日志清理工具，建议选择 winston-rotating-file 类似工具来完成清理，避免日志存放过多，导致磁盘空间不足\n\n\n\u003ch2 align=\"center\"\u003eQuick Start\u003c/h2\u003e\n\n### 1. 安装\n```bash\nnpm install --save @tswjs/tsw\n// yarn add @tswjs/tsw\n```\n### 2. 添加配置文件  \n配置文件是 TSW 启动时加载进运行时的配置文件，主要声明需要使用的 [插件](#plugins) 列表。**默认会加载项目根目录下的 `tswconfig.js` 文件，也可以通过启动参数 `-c` 或者 `--config` 来手动指定配置文件路径。**\n\n**注意事项**: 2.0 中没有集成开放平台相关逻辑，而是封装成了一个插件让用户按需使用，详情见[插件](#plugins)章节。\n\n**配置文件示例：**\n```js\nmodule.exports = {\n  plugins: [\n    new MyPlugin({})\n  ]\n}\n```\n**参数列表**:\n|    Name     |     Type    |     default    |   Optional  |  Description  |\n| :-: | :-: | :-: | :-: | :-: |\n|   plugins   | Array\u003c[Plugin](#plugins)\u003e| - |  yes | [插件](#plugins)列表 |\n|   cleanLog  | boolean |  `false` |  yes | 是否关闭默认打印 |\n|   logLevel  | `DEBUG/INFO/WARN/ERROR` |  `DEBUG` |  yes | 设置 log level |\n|   winstonTransports  | Array\u003c[TransportStream](https://github.com/winstonjs/winston-transport/blob/master/index.d.ts)\u003e |  - |  yes | [Winston](#winston-是什么)日志通道 |\n### 3. 启动\n```bash\nnpx @tswjs/tsw ./index.js\n```\n\n**注意事项**：原先 `node --inspect ./index.js` 中的 CLI 参数如 `--inspect` 需要转化为环境变量 `NODE_OPTIONS` 来执行，如 `NODE_OPTIONS=\"--inspect\" npx @tswjs/tsw ./index.js`。\n\n**使用 ts**: 在保证项目有 [ts-node](https://www.npmjs.com/package/ts-node) 依赖包的情况下，按照如下方式执行即可直接加载 ts 文件。\n```bash\nNODE_OPTIONS=\"--require=ts-node/register\" npx @tswjs/tsw ./index.ts\n```\n### CLI (Command Line Interface)\n\n使用 `npx @tswjs/tsw --help` 来获取 CLI 选项。\n\n### Examples\n\n我们提供了一些示例项目以让大家尽快了解该项目。\n\n1. `cd ~`\n2. `git clone https://github.com/Tencent/TSW.git`\n3. `cd TSW`\n\n#### Koa\n\n1. `cd examples/koa`\n1. `yarn`\n1. `yarn serve` 或者 `npm run serve`\n1. `curl -v localhost:4443/path/to/foo -X POST -d \"hello, server\"`\n\n\u003ch2 align=\"center\"\u003ePlugins\u003c/h2\u003e\n\n### 插件是什么？\n\nTSW 核心的实现方式是 Hack NodeJS 自身的 `http.request` 以及 `http.createServer`， 以此来实现抓包机制。在服务器处理请求的前后，在服务器向其他服务器发包的前后，等等，都会有相应的事件抛出，以供用户来进行自定义处理。**为了让用户更加方便地复用、传播这样一组组自定义处理，我们将他们抽象出来，形成了插件机制。**\n\n### 一个最简单的插件\n\n```js\nexport.modules = class MyPlugin() {\n  constructor() {\n    this.name = \"MyPlugin\"\n  }\n\n  async init(eventBus, config) {\n    eventBus.on(\"RESPONSE_CLOSE\", (payload) =\u003e {\n      console.log(payload);\n    })\n  }\n}\n```\n\n`init` 方法是必须的，这个方法在插件加载开始时会被调用，可以是同步也可以是异步。\n\n#### `eventBus`\n\n`eventBus` 是通过 `new EventEmitter()` 得到的。TSW 核心会在各个关键时机触发上面的事件。\n\n| key | 含义（触发时机） | payload |\n| -- | -- | -- |\n| `DNS_LOOKUP_SUCCESS` | 在每次 DNS 查询成功之后触发 | `string \\| dns.LookupAddress[]` |\n| `DNS_LOOKUP_ERROR` | 在每次 DNS 查询失败之后触发 | `NodeJS.ErrorException` |\n| `RESPONSE_START` | 在每次服务器开始返回响应（执行 `writeHead`）时触发 | `ResponseEventPayload` |\n| `RESPONSE_FINISH` | 在响应结束时（`res.on(\"finish\")`）触发 | `ResponseEventPayload` |\n| `RESPONSE_CLOSE` | 在底层链接关闭时 （`res.on(\"close\")`）触发 | `ResponseEventPayload` |\n| `REQUEST_START` | 在每次服务器接受到新的请求时触发 | `RequestEventPayload` |\n\n\n\u003ch2 align=\"center\"\u003eOpen Platform\u003c/h2\u003e\n\n在默认的情况下，TSW 只是会把所有的日志和抓包内容抓取到并且送到事件总线上，以供 [插件](#插件是什么？) 消费。所以将日志和抓包内容落地查看一般需要用户自己编写插件以及提供存储，使用成本过于高昂。  \n因此，TSW 官方提供了公共的服务平台 [https://tswjs.org](https://tswjs.org)，让用户低成本、更快、更方便地使用 TSW 的特性，详情见 [开放平台使用指引](./docs/use-open-platform.md)。\n\n\u003ch2 align=\"center\"\u003eCluster\u003c/h2\u003e\n\nTSW 2.0 是面对容器化和云原生设计的，所以没有内置 Cluster 相关功能，推荐直接使用容器的健康检查来完成服务的无损重启和故障重启机制。对于没有使用容器化方案的场景来说，我们推荐使用 [pm2](https://github.com/Unitech/pm2) 类似工具来实现多进程模式。\n\n### pm2\n\n#### 使用 Ecosystem File\n\n```js\n// ecosystem.config.json\n\n{\n  \"apps\": [\n    {\n      \"name\": \"app-name\",\n      \"script\": \"built/index.js\",\n      \"interpreter\": \"node\",\n      \"interpreter_args\": \"./node_modules/@tswjs/tsw/dist/cli.js\",\n      // other options\n    }\n  ]\n}\n```\n\n```js\n// package.json\n\n{\n  ...\n  \"scripts\": {\n    \"start\": \"pm2 start ecosystem.config.json\"\n  },\n  ...\n}\n```\n\n\u003ch2 align=\"center\"\u003eWinston\u003c/h2\u003e\n\n### winston 是什么？\n\n`winston` 是一个通用且轻量的日志包。`winston` 支持多个日志通道，并且可以分别定义日志优先级。除了内置的三个日志传输通道[`Console`、 `File`、`HTTP`](https://github.com/winstonjs/winston#common-transport-options)，在 Winston 项目外部还会维护一些[传输模块](https://github.com/winstonjs)。查看 `winston` [官方文档](https://github.com/winstonjs/winston)。\n\nTSW 2.0 支持使用 `winston` 传输通道记录日志信息，用户在配置文件中可以添加 `winston.transports` 实例，日志会落到对应配置中。\n\n### 一个简单的示例\n\n使用 `winston` 记录 `error` 级别 以及 `debug` 级别以下的日志信息到对应文件中，当前 `config` 文件配置如下：\n\n```js\nmodule.exports = {\n  winstonTransports: [\n    new winston.transports.File({ filename: 'error.log', level: 'error'}),\n    new winston.transports.File({ filename: 'debug.log', level: 'debug'})\n  ]\n}\n```\n\n**日志记录**\n\n![log](./static/images/winston-log.png)\n\n\u003ch2 align=\"center\"\u003eUsers\u003c/h2\u003e\n\n[![tsw](./static/images/user/01.png)](https://qzone.qq.com/ \"QQ空间\")　　　　![tsw](./static/images/user/02.png)　　　　[![tsw](./static/images/user/03.png)](https://www.weiyun.com/ \"腾讯微云\")　　　\n　  \n　  \n[![tsw](./static/images/user/04.png)](https://fm.qq.com/ \"企鹅FM\")　　　　[![tsw](./static/images/user/05.png)](https://www.weishi.com/ \"微视\")　　　　![tsw](./static/images/user/06.png)　　　　\n　  \n　  \n[![tsw](./static/images/user/07.png)](http://vip.qq.com/ \"QQ会员\")　　　　[![tsw](./static/images/user/08.png)](http://egame.qq.com/ \"企鹅电竞\")　　　　[![tsw](./static/images/user/09.png)](http://ac.qq.com/ \"QQ动漫\")　　　　\n　  \n　  \n[![tsw](./static/images/user/10.png)](https://buluo.qq.com/ \"QQ兴趣部落\")　　　　[![tsw](./static/images/user/11.png)](http://now.qq.com/ \"NOW直播\")　　　　![tsw](./static/images/user/12.png)　　　　\n　  \n　  \n![tsw](./static/images/user/13.png)　　　　[![tsw](./static/images/user/14.png)](http://yundong.qq.com/ \"QQ运动\")　　　　![tsw](./static/images/user/15.png)　　　　\n　  \n　  \n[![tsw](./static/images/user/16.png)](https://mp.qq.com/ \"QQ公众平台\")　　　　[![tsw](./static/images/user/17.png)](https://office.qq.com/ \"TIM\")　　　　[![tsw](./static/images/user/18.png)](http://tianqi.qq.com/index.htm \"腾讯天气\")　　　　\n　  \n　  \n[![tsw](./static/images/user/19.png)](https://ke.qq.com/ \"腾讯课堂\")　　　　[![tsw](./static/images/user/20.png)](https://cloud.tencent.com/ \"腾讯云\")　　　　[![tsw](./static/images/user/21.png)](https://y.qq.com/ \"QQ音乐\")　　　　\n　  \n　  \n[![tsw](./static/images/user/22.png)](https://kg.tencent.com/ \"全民K歌\")　　　　![tsw](./static/images/user/23.png)　　　　[![tsw](./static/images/user/24.png)](http://e.qq.com/ads/ \"广点通\")　　　　\n　  \n　  \n[![tsw](./static/images/user/25.png)](http://open.qq.com/ \"腾讯开放平台\")　　　　[![tsw](./static/images/user/26.png)](http://kk.qq.com/ \"企鹅看看\")　　　　[![tsw](./static/images/user/27.png)](http://sports.qq.com/ \"腾讯体育\") \n\n\u003ch2 align=\"center\"\u003eLicense\u003c/h2\u003e\n\nTencent Server Web 的开源协议为 MIT, 详情参见 [LICENSE](https://github.com/Tencent/TSW/blob/master/LICENSE) 。\n","funding_links":[],"categories":["TypeScript","Projects List"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTencent%2FTSW","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTencent%2FTSW","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTencent%2FTSW/lists"}