{"id":28684892,"url":"https://github.com/node-modules/tcp-base","last_synced_at":"2025-06-14T03:07:32.845Z","repository":{"id":58384456,"uuid":"68363545","full_name":"node-modules/tcp-base","owner":"node-modules","description":"TCP client base","archived":false,"fork":false,"pushed_at":"2023-08-30T14:11:47.000Z","size":49,"stargazers_count":27,"open_issues_count":2,"forks_count":11,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-06-08T21:35:51.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/node-modules.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.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":"2016-09-16T08:20:09.000Z","updated_at":"2025-03-22T09:37:35.000Z","dependencies_parsed_at":"2024-06-18T15:23:18.182Z","dependency_job_id":null,"html_url":"https://github.com/node-modules/tcp-base","commit_stats":{"total_commits":23,"total_committers":9,"mean_commits":"2.5555555555555554","dds":0.7391304347826086,"last_synced_commit":"a13f7c099358af78073bc9e75484aa7bd7486e95"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/node-modules/tcp-base","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Ftcp-base","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Ftcp-base/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Ftcp-base/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Ftcp-base/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-modules","download_url":"https://codeload.github.com/node-modules/tcp-base/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Ftcp-base/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259752078,"owners_count":22905972,"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":[],"created_at":"2025-06-14T03:07:31.999Z","updated_at":"2025-06-14T03:07:32.840Z","avatar_url":"https://github.com/node-modules.png","language":"JavaScript","readme":"# tcp-base\n\nA base class for tcp client with basic functions\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/node-modules/tcp-base/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/tcp-base/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/tcp-base.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/tcp-base\n[codecov-image]: https://codecov.io/gh/node-modules/tcp-base/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/node-modules/tcp-base\n[download-image]: https://img.shields.io/npm/dm/tcp-base.svg?style=flat-square\n[download-url]: https://npmjs.org/package/tcp-base\n\n## Install\n\n```bash\nnpm install tcp-base --save\n```\n\nNode.js \u003e= 6.0.0 required\n\n## Usage\n\nA quick guide to implement a tcp echo client\n\nClient:\n\n```js\nconst TCPBase = require('tcp-base');\n\n/**\n * A Simple Protocol:\n *   (4B): request id\n *   (4B): body length\n *   ------------------------------\n *   body data\n */\nclass Client extends TCPBase {\n  getHeader() {\n    return this.read(8);\n  }\n\n  getBodyLength(header) {\n    return header.readInt32BE(4);\n  }\n\n  decode(body, header) {\n    return {\n      id: header.readInt32BE(0),\n      data: body,\n    };\n  }\n\n  // heartbeat packet\n  get heartBeatPacket() {\n    return Buffer.from([ 255, 255, 255, 255, 0, 0, 0, 0 ]);\n  }\n}\n\nconst client = new Client({\n  host: '127.0.0.1',\n  port: 8080,\n});\n\nconst body = Buffer.from('hello');\nconst data = Buffer.alloc(8 + body.length);\ndata.writeInt32BE(1, 0);\ndata.writeInt32BE(body.length, 4);\nbody.copy(data, 8, 0);\n\nclient.send({\n  id: 1,\n  data,\n  timeout: 5000,\n}, (err, res) =\u003e {\n  if (err) {\n    console.error(err);\n  }\n  console.log(res.toString()); // should echo 'hello'\n});\n```\n\nServer:\n\n```js\n'use strict';\n\nconst net = require('net');\n\nconst server = net.createServer(socket =\u003e {\n  let header;\n  let bodyLen;\n\n  function readPacket() {\n    if (bodyLen == null) {\n      header = socket.read(8);\n      if (!header) {\n        return false;\n      }\n      bodyLen = header.readInt32BE(4);\n    }\n\n    if (bodyLen === 0) {\n      socket.write(header);\n    } else {\n      const body = socket.read(bodyLen);\n      if (!body) {\n        return false;\n      }\n      socket.write(Buffer.concat([ header, body ]));\n    }\n    bodyLen = null;\n    return true;\n  }\n\n  socket.on('readable', () =\u003e {\n    try {\n      let remaining = false;\n      do {\n        remaining = readPacket();\n      }\n      while (remaining);\n    } catch (err) {\n      console.error(err);\n    }\n  });\n});\nserver.listen(8080);\n```\n\n[MIT](LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Ftcp-base","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-modules%2Ftcp-base","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Ftcp-base/lists"}