npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sknx/transport

v2.0.0

Published

Node IPC transport

Downloads

3

Readme

@sknx/transport

Node IPC transport compatible with cluster.

Basic example

if (cluster.isPrimary) {
  const server = new TransportServer();
  server.on('client', (client) => {
    console.log('server: new client');
    server.dispatch(client, 'hello', 'world');
  });
  server.handle('test', (data) => {
    console.log('server handle test: %o', data); // { foo: 'bar' }
    return ['any', 'data'];
  });
  await server.listen();
} else {
  const client = new TransportClient();
  client.handle('hello', (data) => {
    console.log('client handle hello: %o', data); // 'world'
  });
  await client.connect();
  client.dispatch('test', { foo: 'bar' }).then((data) => {
    console.log('client test result: %o', data); // ['any', 'data']
  });
}

Server usage

const server = new TransportServer({
  name: 'my-server',
  timeout: 1000,
});
await server.listen();

| Param | Type | Default | Description | | --------- | ------ | ---------- | ------------------------------------------------------------------------------------------------------- | | name | string | parent pid | is used for communication between server and client | | timeout | number | 0 | the timeout of dispatches, if zero then no timeout, if negative then not awaiting dispatch response |

server
  .on('ready', () => console.log('server ready'))
  .on('close', () => console.log('server close'))
  .on('error', (error) => console.error(error))
  .on('client', (client) => console.log('new client'));

| Event | Description | | -------- | ---------------------------------------------------------------- | | ready | emitted when server is ready (before server.listen() resolved) | | close | emitted when server is closed | | error | emitted when an error occurs | | client | emitted when new client is connected |

// Server could be restarted
await server.close();
await server.listen();
// Server broadcasting implementation
const clients = server.getClients();
clients.forEach((client) => server.dispatch(client, 'event', Date.now(), -1));

Client usage

const client = new TransportClient({
  name: 'ipc',
  reconnectAttempts: 5,
  reconnectDelay: 1000,
  timeout: 1000,
});
await client.connect();

| Param | Type | Default | Description | | ------------------- | ------ | ---------- | ------------------------------------------------------------------------------------------------------- | | name | string | parent pid | is used for communication between server and client | | reconnectAttempts | number | 0 | max reconnection attempts, if zero then no reconnection, if negative then infinite reconnection | | reconnectDelay | number | 1000 | delay between reconnection attempts in ms | | timeout | number | 0 | the timeout of dispatches, if zero then no timeout, if negative then not awaiting dispatch response |

client
  .on('ready', () => console.log('client ready'))
  .on('close', () => console.log('client close'))
  .on('error', (error) => console.error(error));

| Event | Description | | ------- | ----------------------------------------------------------------- | | ready | emitted when client is ready (before client.connect() resolved) | | close | emitted when client is closed and reconnections exceeded | | error | emitted when an error occurs |

Client / Server communication

const server = new TransportServer();
server
  .handle<{ foo: string }>('test1', (data) => {
    console.log('test1: %o', data);
    return 'test1';
  })
  .handle<string | null>('test2', async (data) => {
    console.log('test2: %o', data);
    if (!data) throw new Error('No data');
    return 'test2';
  })
  .handle<number | null>('test3', async (data, { resolve, reject }) => {
    console.log('test3: %o', data);
    if (!data) reject(new Error('No data'));
    resolve('test3');
  })
  .handle<number>('test4', (data, { resolve }) => {
    console.log('test4: %o', data);
    setTimeout(() => resolve('test4'), data);
  });
await server.listen();
//
const client = new TransportClient();
await client.connect();
const data = await client.dispatch<string>('test1', { foo: 'bar1' });
console.log('test1 result: %o', data);
await client
  .dispatch<string>('test2', 'bar2')
  .then((data) => console.log('test2 result: %o', data))
  .catch((error) => console.log('test2 error: %o', error.message));
await client
  .dispatch<string>('test3', 0)
  .then((data) => console.log('test3 result: %o', data))
  .catch((error) => console.log('test3 error: %o', error.message));
await client
  .dispatch<string>('test4', 1000, 100)
  .then((data) => console.log('test4 result: %o', data))
  .catch((error) => console.log('test4 error: %o', error.message));
await client
  .dispatch<string>('test4', 50, 100)
  .then((data) => console.log('test4 result: %o', data))
  .catch((error) => console.log('test4 error: %o', error.message));
test1: { foo: 'bar1' }
test1 result: 'test1'
test2: 'bar2'
test2 result: 'test2'
test3: 0
test3 error: 'No data'
test4: 1000
test4 error: 'Dispatch timed out'
test4: 50
test4 result: 'test4'

Only one handler can be set for each event, since the result of the handler execution is sent in the response