{"id":24847919,"url":"https://github.com/luckydrq/tcp-net","last_synced_at":"2025-10-14T20:30:25.219Z","repository":{"id":63215628,"uuid":"164439191","full_name":"luckydrq/tcp-net","owner":"luckydrq","description":"Build tcp applications in a stable and elegant way","archived":false,"fork":false,"pushed_at":"2023-07-20T09:56:52.000Z","size":28,"stargazers_count":49,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-18T08:17:07.820Z","etag":null,"topics":["socket","tcp","tcp-client","tcp-server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luckydrq.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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":"2019-01-07T13:47:51.000Z","updated_at":"2025-08-06T02:41:24.000Z","dependencies_parsed_at":"2025-01-31T11:47:40.643Z","dependency_job_id":null,"html_url":"https://github.com/luckydrq/tcp-net","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/luckydrq/tcp-net","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Ftcp-net","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Ftcp-net/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Ftcp-net/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Ftcp-net/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luckydrq","download_url":"https://codeload.github.com/luckydrq/tcp-net/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Ftcp-net/sbom","scorecard":{"id":602863,"data":{"date":"2025-08-11","repo":{"name":"github.com/luckydrq/tcp-net","commit":"779c4ced4076a2351a26013dc7ec5f3ebb00737d"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.5,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Code-Review","score":0,"reason":"Found 0/21 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 3 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-21T00:51:05.459Z","repository_id":63215628,"created_at":"2025-08-21T00:51:05.459Z","updated_at":"2025-08-21T00:51:05.459Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279020905,"owners_count":26086948,"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","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["socket","tcp","tcp-client","tcp-server"],"created_at":"2025-01-31T11:34:59.216Z","updated_at":"2025-10-14T20:30:24.885Z","avatar_url":"https://github.com/luckydrq.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# @luckydrq/tcp-net\n\n编写稳定可靠的TCP长连接应用。\n\n封装TCP连接的拆解包逻辑，支持`Length-Based`和`Line-Based`两种协议格式。\n\n**注意，本模块只支持node 8.x以上。**\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![NPM download][download-image]][download-url]\n[![Gitter][gitter-image]][gitter-url]\n\n[npm-image]: https://img.shields.io/npm/v/@luckydrq/tcp-net.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@luckydrq/tcp-net\n[travis-image]: https://img.shields.io/travis/luckydrq/tcp-net.svg?style=flat-square\n[travis-url]: https://travis-ci.org/luckydrq/tcp-net\n[codecov-image]: https://img.shields.io/codecov/c/github/luckydrq/tcp-net.svg?style=flat-square\n[codecov-url]: https://codecov.io/gh/luckydrq/tcp-net\n[david-image]: https://img.shields.io/david/luckydrq/tcp-net.svg?style=flat-square\n[david-url]: https://david-dm.org/luckydrq/tcp-net\n[snyk-image]: https://snyk.io/test/npm/@luckydrq/tcp-net/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/@luckydrq/tcp-net\n[download-image]: https://img.shields.io/npm/dm/@luckydrq/tcp-net.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@luckydrq/tcp-net\n[gitter-image]: https://img.shields.io/gitter/room/luckydrq/tcp-net.svg?style=flat-square\n[gitter-url]: https://gitter.im/luckydrq/tcp-net\n\n## Feature\n\n- [x] 长连接\n- [x] 连接保持（心跳检测）\n- [x] 支持多请求多响应\n- [x] 可扩展协议\n- [x] 可扩展编解码器\n\n## Protocol\n\n我们对一个tcp包协议格式进行了如下约定：\n\n```\n|-----------------------------------------------|\n| 1 byte  | 4 bytes  | 1 byte     | N bytes     |\n|-----------------------------------------------|\n| reqType | packetId | packetType | custom body |\n|-----------------------------------------------|\n```\n\n- reqType: Number, 请求类型。0 - 请求 1 - 响应\n- packetId: Number, 包id。\n- packetType: Number, 包类型。0 - 普通包 1 - 心跳包\n- custom body: 自定义包体。\n\n其中，自定义包体就是用户发送的数据，它可以支持`Length-Based`和`Line-Based`两种子协议。\n\n### Length-Based Protocol\n\n默认内置。适用于用户定义的包长度是确定的场景。原理很简单，先用一个field标识包体的长度，紧接着就是相应长度的包体。field本身长度支持自定义设置，默认是4字节。\n\n```\n|------------------|---------------|\n|      4 bytes     |    N bytes    |\n|------------------|---------------|\n| PACKET LENGTH(N) | HEADER | BODY |\n|------------------|---------------|\n```\n\n### Line-Based Protocol\n\n适用于用户定义的包长度不确定的场景。它用一种边界符来表示包的结束。最典型的例子，`http`协议格式就是`Line-Based`的协议，它的边界符（或者说分隔符）是`CRLF(\\r\\n)`。边界符支持自定义设置，默认是`CRLF`。\n\n```\n|------------------|-----------|\n|      N bytes     | delimiter |\n|------------------|-----------|\n|    PACKET BODY   |    \\r\\n   |\n|------------------|-----------|\n```\n\n## Install\n```\n$ npm i @luckydrq/tcp-net -S\n```\n\n## Examples\n\n### Basic\n\nclient和server应该使用相同的协议，保持拆解包逻辑一致。\n\n#### server\n```js\nconst net = require('net');\nconst { Connection } = require('@luckydrq/tcp-net');\n\nnet.createServer(socket =\u003e {\n  const conn = new Connection({\n    socket,\n    // protocol: new LengthBasedProtocol(),  default\n  });\n\n  // 发送一个packet\n  conn.write('hello world');\n\n  // 收到packet\n  conn.on('packet', ({ packetId, body }) =\u003e {\n    console.log(packetId); // 1\n    console.log(JSON.parse(body)); // { name: 'xuezu' }\n  });\n}).listen();\n\n```\n\n#### client\n\n```js\nconst { Connection } = require('@luckydrq/tcp-net');\nconst conn = new Connection({ host, port });\n\n// 发送一个packet\nconn.write({ name: 'xuezu' });\n\n// 收到一个packet\nconn.on('packet', ({ packetId, body }) =\u003e {\n  console.log(packetId); // 1\n  console.log(body); // hello world\n});\n```\n\n### Heartbeat\n\n内置了心跳检测，默认每隔5秒发起一次心跳检测，响应超时时间为3秒，超时3次则关闭连接。支持配置，在构造函数中传入：\n\n```js\nconst conn = new Connection({\n  heartbeatInterval: 5 * 1000, // 心跳间隔\n  heartbeatTimeout: 3 * 1000, // 超时时间\n  heartbeatLimit: 3, // 允许超时次数\n});\n\nconn.on('heartbeatTimeout', () =\u003e {\n  console.log('连接已经关闭');\n});\n```\n\n### Transcoder\n\n通常需要对数据做编解码，本例子展示如何接入一个自定义编解码器。如果不提供，默认[只做基本的序列化](https://github.com/luckydrq/tcp-net/blob/master/lib/protocol.js)。\n\n自定义实现一个transCoder，只需要实现`.encode`和`.decode`方法即可，也可以只实现其中之一，不过通常编解码逻辑都是对应的。\n\n#### server\n\n```js\nconst transCoder = {\n  encode(buf) {\n    // 调用hessian编码\n    // 注意，需要返回Buffer对象\n    return hessian.encode(buf);\n  },\n\n  decode(data) {\n    // 可以返回任意类型的数据\n    return hessian.decode(buf);\n  },\n};\n\nnet.createServer(socket =\u003e {\n  const conn = new Connection({\n    socket,\n    protocol: new LengthBasedProtocol({ transCoder }), // 在协议中传入transCoder\n  });\n\n  // 收到packet\n  conn.on('packet', ({ body }) =\u003e console.log(body));\n}).listen();\n```\n\n#### client\n\n```js\nconst conn = new Connection({\n  host,\n  port,\n  protocol: new LengthBasedProtocol({ transCoder }),\n});\n// 发一个packet\nconn.write({ name: 'xuezu' });\n```\n\n### [更多例子](https://github.com/luckydrq/tcp-net/blob/master/test/index.test.js)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluckydrq%2Ftcp-net","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluckydrq%2Ftcp-net","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluckydrq%2Ftcp-net/lists"}