Git Product home page Git Product logo

create-local-domain-socket's Introduction

create-local-domain-socket

Build Status Build status Coverage Status npm version License

A helper function to create cross-platform local domain sockets (UNIX domain sockets on UNIX, and named pipes polyfill on Windows).

Usage

createLocalDomainSocket

import createLocalDomainSocket from 'create-local-domain-socket';
import http from 'http';

/* create a whatever you want server */
const server = http.createServer((req, res) => {
  const body = http.STATUS_CODES[426];
  res.writeHead(426, {
    'Content-Length': body.length,
    'Content-Type': 'text/plain'
  });
  res.end(body);
});

createLocalDomainSocket(server, '/tmp/test.sock', (err) => {
  if (err) { console.error(err); }
  else { console.log('socket server started'); }
});

If you prefer using promise:

const server = http.createServer();
createLocalDomainSocket(server, '/tmp/test.sock')
  .then(() => console.log('socket server started'))
  .catch((err) => console.error(err))
;

Arguments

  1. server <Object>: A server object. Should include a .listen() and .on('error') methods to start and catch errors
  2. path <String>: Local domain path
  3. callback <Function> (Optional): A callback function. The first argument is an Error object if listen failed. If callback is undefined, it will return a promise

ensureLocalDomainPath

A tiny helper function to ensure local domain path. On Windows, it will convert to named pipe path instead of local domain path.

Example on Windows

import { ensureLocalDomainPath } from 'create-local-domain-socket';

const path = ensureLocalDomainPath('/test');
console.log(path); /* "\\\\.\\pipe\\test" */

Installation

npm install --save create-local-domain-socket

Example to integrate with ws

import WebSocket from 'ws';
import createLocalDomainSocket from 'createLocalDomainSocket';
import http from 'http';

const server = http.createServer((req, res) => {
  const body = http.STATUS_CODES[426];
  res.writeHead(426, {
    'Content-Length': body.length,
    'Content-Type': 'text/plain'
  });
  res.end(body);
});

createLocalDomainSocket(server, path, (err) => {
  if (err) { done.fail(err); }
  else {
    const wss = new WebSocket.Server({ server });
    const ws = new WebSocket(`ws+unix://${path}`);
    ws.on('message', (message) => {
      /* do sth... */
    });
    wss.on('connection', (client) => {
      client.send('sth');
    });
  }
});

License

MIT

create-local-domain-socket's People

Contributors

cap32 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.