userland implementation of https://github.com/joyent/node/issues/5243