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

sync-socket

v1.1.0

Published

A synchronous interface to Node's net.Socket

Downloads

37

Readme

SyncSocket

This Node.js module exposes a no-dependency class called SyncSocket, which acts as a synchronous interface to Node's net.Socket instance, which is normally only asynchronous.

This module exposes many of the same methods and properties a normal net.Socket has.

Considerations

SyncSocket is slow. Not unusably slow, but you should have a good reason to want to use it. This was made for the rare case you need blocking read/writes in your Node.js application.

In addition, another one of my modules, netlinkwrapper exposes a similar synchronous socket. However that module requires node-gyp to compile. SyncSocket does not require node-gyp. In fact, it has no dependencies.

How Does it Work?

tl;dr: I abuse child_process.execFileSync.

I've done some pretty thorough research into how Node's net.Socket module works, and you can't make it synchronous without rewriting it in C++ via node-gyp, or using some modules like fibers that in turn would depend on node-gyp anyways.

With that in mind I've devised this hack that abuses child processes.

One process, the worker, is spawned that uses the normal net module doing read/writes asynchronously as you should to net.Sockets. It then creates a TCP server to allow requests to its net.Socket.

Then another file, the query script, acts as a script that will request something from the worker, and spit the result out to stdout once it has the results, again gotten asynchronously.

Now from our main thread we can abuse child_process.execFileSync, which executes the query script, which then queries our worker process, and exits with the result.

Obviously this is a ton of overhead just to avoid callbacks. But it works.

Benchmarks

I've ran some tests, and SyncSocket is about 20 times slower in doing read/writes than my competing module netlinkwrapper. So if node-gyp is not an issue to you, use that instead. Or really just use net.Socket.