All posts tagged nodejs

I recently read Semver in node.js and npm, which aims to shed some light on the oft misunderstood operators ~ and ^ in semver. The very short version is that:

  • ~ with just a major version will match any version with that major version.
  • ~ with a major and minor version will match any patch version of the minor version.
  • ^ will always match versions between the specified version and the next major version.

So ~1 would match 1.6.2, but ~1.4 would not; ^1 would match 1.6.2, and ^1.4 would too.

A concise overview of semver can be found at semver ftw. Plenty more detail on semver in node can be found on node-semver on github. If you look closely, you’ll notice that for the ^ and ~ operators, versions below 1.0.0 are treated differently: the first non-zero component is considered to potentially introduce breaking changes. This means that ^0.2.3 would not match version 0.3.0. Some consider this a bad idea, and I would have to agree since it complicates the system and reduces the chance that developers will use it properly. The problems with major version 0 as a special case are discussed in Kill the special pre-1.0.0 meaning for ^ & ~ and 1.0.0 is not just a number

If semver seems overly complicated, there is always slimver, which describes a simplified subset of semver. Slimver excludes prerelease and build metadata and limits version numbers to 65535, which allows easy encoding as a single numeric value for easy comparison. It also simplifies the range expressions for ~ and ^ so that their meaning is identical and versions in the 0.x.x and 0.0.x range are treated the same way as others, making it far more likely that developers will get what they are expecting when using either operator.

I was browsing through Node.js® Reactions and came across Setting highWaterMark way too low: (trigger warning: someone does something silly and looks like they got a bit bruised in the head/knees/shins areas).

I found it immediately hilarious, then realized there was something called highWaterMark of which I was utterly ignorant! Seeking enlightenment, I set out into the open Internet. I did not have far to search, and soon found myself alighting gently on A New Streaming API for Node v0.10, and was instantly swept up in reading. I had not read long when my eyes fell upon a most wonderful sentiment:

“Being better than the next guy isn’t enough; we have to be the best imaginable.”

I had to stop, smile, and let that sink in for a while; highWaterMark all but forgotten*. This is a world I am happy to be a part of.

* I did later find highWaterMark; it is an option for streams that sets the maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.