{"id":28684878,"url":"https://github.com/node-modules/connection","last_synced_at":"2025-06-14T03:07:26.476Z","repository":{"id":56517144,"uuid":"139924239","full_name":"node-modules/connection","owner":"node-modules","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-19T16:25:06.000Z","size":35,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-06-11T18:58:14.606Z","etag":null,"topics":[],"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/node-modules.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-06T02:38:44.000Z","updated_at":"2024-06-19T16:25:09.000Z","dependencies_parsed_at":"2022-08-15T20:10:49.517Z","dependency_job_id":null,"html_url":"https://github.com/node-modules/connection","commit_stats":null,"previous_names":["node-modules/connection","alibaba-archive/connection"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/node-modules/connection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fconnection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fconnection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fconnection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fconnection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-modules","download_url":"https://codeload.github.com/node-modules/connection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fconnection/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:24.497Z","updated_at":"2025-06-14T03:07:26.467Z","avatar_url":"https://github.com/node-modules.png","language":"JavaScript","readme":"# connection\n\n[connection](https://github.com/node-modules/connection) socket wrapper\n\n[![NPM version][npm-image]][npm-url]\n[![CI](https://github.com/node-modules/connection/actions/workflows/nodejs.yml/badge.svg?branch=master)](https://github.com/node-modules/connection/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/connection.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/connection\n[codecov-image]: https://codecov.io/gh/node-modules/connection/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/node-modules/connection\n[snyk-image]: https://snyk.io/test/npm/connection/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/connection\n[download-image]: https://img.shields.io/npm/dm/connection.svg?style=flat-square\n[download-url]: https://npmjs.org/package/connection\n\n## Usage\n\n### Client Socket\n\n```js\nconst net = require('net');\nconst awaitFirst = require('await-first');\nconst Connection = require('connection');\n\nconst Decoder = require('sofa-bolt-node/lib/decoder');\nconst Encoder = require('sofa-bolt-node/lib/encoder');\n// bolt protocol example\nconst protocol = {\n  name: 'Rpc',\n  encoder: opts =\u003e new Encoder(opts),\n  decoder: opts =\u003e new Decoder(opts),\n};\n\nasync function createConnection(hostname, port) {\n  const socket = net.connect(port, hostname);\n  await awaitFirst(socket, [ 'connect', 'error' ]);\n  return new Connection({\n    logger: console,\n    socket,\n    protocol,\n  });\n}\n\nconst conn = await createConnection('127.0.0.1', 12200);\n\nconn.writeRequest({\n  targetAppName: 'foo',\n  args: [ 'peter' ],\n  serverSignature: 'com.alipay.sofa.rpc.quickstart.HelloService:1.0',\n  methodName: 'sayHello',\n  methodArgSigs: [ 'java.lang.String' ],\n  requestProps: null,\n});\n```\n\n### Server Socket\n\n```js\nconst Connection = require('connection');\nconst server = net.createServer();\nserver.listen(port);\n\nserver.on('connection', sock =\u003e {\n  const conn = new Connection({\n    logger: console,\n    socket: sock,\n    protocol,\n  });\n  \n  conn.on('request', req =\u003e {\n    conn.writeResponse(req, {\n      error: null,\n      appResponse: 'hello, peter',\n      responseProps: null,\n    });\n  });\n});\n```\n\n[More example](./example)\n\n### API\n\n- oneway() - one way call\n- async writeRequest(req) - write request and wait response\n- async writeResponse(req, res) - write response\n- async close() - wait all pending request done and destroy the socket\n- async forceClose() - abort all pending request and destroy the socket\n- get protocolOptions() - encoder/decoder constructor options, can be overwrite when custom protocol\n\n### Protocol implement\n\n```typescript\ninterface Request {\n  /**\n   * If request is oneway, shoule set to true\n   */\n  oneway: boolean,\n  /**\n   * writeRequest will use the timeout to set the timer\n   */\n  timeout: number,\n  /**\n   * request packet type, request|heartbeat|response \n   */\n  packetType: string,\n}\n\ninterface Response {\n  packetId: number,\n}\n\ninterface Encoder extends Transform {\n  /**\n   * write request to socket\n   * Connection#writeRequest and Connection#oneway will call the function.\n   * @param {number} id - the request id\n   * @param {Object} req - the request object should be encoded\n   * @param {Function} cb - the encode callback\n   */\n  writeRequest(id: number, req: object, cb);\n  /**\n   * write response to socket\n   * Connection#writeResponse will call the function.\n   * @param {Object} req - the request object\n   * @param {Object} res - the response object should be encoded\n   * @param {Function} cb - the encode callback\n   */\n  writeResponse(req: object, res: object, cb);\n}\n\ninterface Decoder extends Writable {\n  // events\n  // - request emit when have request packet\n  // - heartbeat emit when have heartbeat packet\n  // - response emit when have response packet\n}\n\ninterface Protocol {\n  name: string;\n  encode(options: any): Encoder;\n  decode(options: any): Decoder;\n}\n```\n\n## License\n\n[MIT](LICENSE)\n\n\u003c!-- GITCONTRIBUTOR_START --\u003e\n\n## Contributors\n\n|[\u003cimg src=\"https://avatars.githubusercontent.com/u/6897780?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003ekillagu\u003c/b\u003e\u003c/sub\u003e](https://github.com/killagu)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/1207064?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003egxcsoccer\u003c/b\u003e\u003c/sub\u003e](https://github.com/gxcsoccer)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/360661?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003epopomore\u003c/b\u003e\u003c/sub\u003e](https://github.com/popomore)\u003cbr/\u003e|[\u003cimg src=\"https://avatars.githubusercontent.com/u/17469139?v=4\" width=\"100px;\"/\u003e\u003cbr/\u003e\u003csub\u003e\u003cb\u003eweijiafu14\u003c/b\u003e\u003c/sub\u003e](https://github.com/weijiafu14)\u003cbr/\u003e|\n| :---: | :---: | :---: | :---: |\n\n\nThis project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed Jun 19 2024 17:29:25 GMT+0800`.\n\n\u003c!-- GITCONTRIBUTOR_END --\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fconnection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-modules%2Fconnection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fconnection/lists"}