{"id":21937622,"url":"https://github.com/cap32/create-local-domain-socket","last_synced_at":"2025-04-22T12:12:43.965Z","repository":{"id":57210622,"uuid":"109509232","full_name":"Cap32/create-local-domain-socket","owner":"Cap32","description":"A helper function to create cross-platform local domain sockets","archived":false,"fork":false,"pushed_at":"2018-09-23T13:42:07.000Z","size":58,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T12:12:25.009Z","etag":null,"topics":["cross-platform","helper","ipc","named-pipes","socket","unix","unix-domain-socket","windows"],"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/Cap32.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-04T16:04:16.000Z","updated_at":"2023-03-04T05:34:31.000Z","dependencies_parsed_at":"2022-08-31T04:01:31.673Z","dependency_job_id":null,"html_url":"https://github.com/Cap32/create-local-domain-socket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fcreate-local-domain-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fcreate-local-domain-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fcreate-local-domain-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fcreate-local-domain-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cap32","download_url":"https://codeload.github.com/Cap32/create-local-domain-socket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250237832,"owners_count":21397401,"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":["cross-platform","helper","ipc","named-pipes","socket","unix","unix-domain-socket","windows"],"created_at":"2024-11-29T01:20:49.092Z","updated_at":"2025-04-22T12:12:43.945Z","avatar_url":"https://github.com/Cap32.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# create-local-domain-socket\n\n[![Build Status](https://travis-ci.org/Cap32/create-local-domain-socket.svg?branch=master)](https://travis-ci.org/Cap32/create-local-domain-socket)\n[![Build status](https://ci.appveyor.com/api/projects/status/g0wa7fu7n8fnpfc2/branch/master?svg=true)](https://ci.appveyor.com/project/Cap32/create-local-domain-socket/branch/master)\n[![Coverage Status](https://coveralls.io/repos/github/Cap32/create-local-domain-socket/badge.svg?branch=master)](https://coveralls.io/github/Cap32/create-local-domain-socket?branch=master)\n[![npm version](https://badge.fury.io/js/create-local-domain-socket.svg)](https://badge.fury.io/js/create-local-domain-socket)\n[![License](https://img.shields.io/badge/license-MIT_License-brightgreen.svg?style=flat)](https://github.com/Cap32/create-local-domain-socket/blob/master/LICENSE.md)\n\nA helper function to create cross-platform local domain sockets (UNIX domain sockets on UNIX, and named pipes polyfill on Windows).\n\n\n## Usage\n\n### createLocalDomainSocket\n\n```js\nimport createLocalDomainSocket from 'create-local-domain-socket';\nimport http from 'http';\n\n/* create a whatever you want server */\nconst server = http.createServer((req, res) =\u003e {\n  const body = http.STATUS_CODES[426];\n  res.writeHead(426, {\n    'Content-Length': body.length,\n    'Content-Type': 'text/plain'\n  });\n  res.end(body);\n});\n\ncreateLocalDomainSocket(server, '/tmp/test.sock', (err) =\u003e {\n  if (err) { console.error(err); }\n  else { console.log('socket server started'); }\n});\n```\n\nIf you prefer using promise:\n\n```js\nconst server = http.createServer();\ncreateLocalDomainSocket(server, '/tmp/test.sock')\n  .then(() =\u003e console.log('socket server started'))\n  .catch((err) =\u003e console.error(err))\n;\n```\n\n#### Arguments\n\n1. `server` \\\u003cObject\\\u003e: A server object. Should include a `.listen()` and `.on('error')` methods to start and catch errors\n2. `path` \\\u003cString\\\u003e: Local domain path\n3. `callback` \\\u003cFunction\\\u003e (Optional): A callback function. The first argument is an `Error` object if listen failed. If `callback` is `undefined`, it will return a promise\n\n\n### ensureLocalDomainPath\n\nA tiny helper function to ensure local domain path. On Windows, it will convert to named pipe path instead of local domain path.\n\n#### Example on Windows\n\n```js\nimport { ensureLocalDomainPath } from 'create-local-domain-socket';\n\nconst path = ensureLocalDomainPath('/test');\nconsole.log(path); /* \"\\\\\\\\.\\\\pipe\\\\test\" */\n```\n\n## Installation\n\n```bash\nnpm install --save create-local-domain-socket\n```\n\n## Example to integrate with [ws](https://github.com/websockets/ws)\n\n```js\nimport WebSocket from 'ws';\nimport createLocalDomainSocket from 'createLocalDomainSocket';\nimport http from 'http';\n\nconst server = http.createServer((req, res) =\u003e {\n  const body = http.STATUS_CODES[426];\n  res.writeHead(426, {\n    'Content-Length': body.length,\n    'Content-Type': 'text/plain'\n  });\n  res.end(body);\n});\n\ncreateLocalDomainSocket(server, path, (err) =\u003e {\n  if (err) { done.fail(err); }\n  else {\n    const wss = new WebSocket.Server({ server });\n    const ws = new WebSocket(`ws+unix://${path}`);\n    ws.on('message', (message) =\u003e {\n      /* do sth... */\n    });\n    wss.on('connection', (client) =\u003e {\n      client.send('sth');\n    });\n  }\n});\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcap32%2Fcreate-local-domain-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcap32%2Fcreate-local-domain-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcap32%2Fcreate-local-domain-socket/lists"}