78 points · by iskin · 9y ago · 78 comments
The nanoid implementation does not really bring anything new to the table except having fewer SLOC (which I personally don't find important). It is also incompatible with https://tools.ietf.org/html/rfc4648#section-5 for no good reason. It is noticable again and again that devs publishing to npm are not up to snuff, I wish I knew why, as this does not happen with other dynamic languages/their code repos.
It absolutely does happen with other languages. It just happens in Node with greater volume because of the greater volume of programmers, low barriers to entry for JavaScript, lack of a rich standard library, the convergence of server focused coders and frontend coders, and lack of consensus around best practices and frameworks for applications.
Kind of the way it's been possible to write decent code in PHP since version 5. Yet the overall ecosystem is still mostly bad.
Lots of need to fashion very basic tools + high volume of inexperience + democracy = A package repo full of nonsense where it's difficult to find the diamonds in the rough.
https://tools.ietf.org/html/rfc4648#page-7
Note that it excludes the use of ~ because it has special meaning in some file systems. Nano ID includes ~. It's a good demonstration of why sometimes a standards comittee is better than a lone developer: A group of people and reviewers are more likely to think of everything.
But then, there are people who'd like to use 62-character alphabet, avoiding any special characters. This package allows them do generate such strings.
> devs publishing to npm are not up to snuff
Indeed, there are tons of packages that use Math.random or take modulo without accounting for bias. This is why I'm glad there are new packages like nanoid appearing, created by people who know what they are doing, and this is why your negativity is unfounded.
Because it's a standard data type. It's supported by the database and the data abstraction layers. I can use other implementations for creating, storing and retrieving UUIDs because they backed by the standard and are interoperable. I don't get these advantages with your ad-hoc approach. The loss of 4 bits do not hurt me.
> this is why your negativity is unfounded
I was criticising the needless deviation from an encoding standard. You talk about how it's good that nanoid picked the correct random algorithm. That just does not fit together.
JS stdlib has always be lacking in almost every possible aspects, and hence people have to reinvent the wheel again and again.
FWIW. I have also always done the exact Base64 UUID op suggested when I need a shortened unique identifier...
I don't understand: is the default implementation of random in node.js flawed? Surely it should produce a uniform distribution…
edit nevermind, found the link to https://gist.github.com/joepie91/7105003c3b26e65efcea63f3db8... just above that paragraph
0 => 0
1 => 1
2 => 2
3 => 0
4 => 1
So, a uniform input will produce more 0s and 1s than it will 2s.
However, there are methods of converting uniform distributions that will preserve uniformity. I'll have to find a good reference.
But I don't like that it's equivalent to UUID v4 but different. The difference doesn't buy anything significant over the standard.
With a pretty small change this could directly generate base64 encoded UUID v4 (without going through a two-step process of generating a UUID and then encoding it: Most characters could be generated randomly from the base64 alphabet. The characters at certain indexes would need to use a restricted set of base64 characters to set certain bits to the values specified by UUID v4.
The scheme allows for 2 billion possible records per second, although if you consider birthday collisions, it comes down to a lot less (but I could re-insert on duplicates). I could later also use some of the random bits to inscribe extra data into the ID.
The bits from the two halves are then interspersed to make sure the first half looks random, and transformed to a 11-character id, for example /^[0-9A-Za-z]{11}$/. I can reject some IDs beforehand if they match a list of bad strings. The result is receiving an id such as "Vxe61yU5Cci" or "WgOUBEO88gb".
EDIT: I'm not criticizing you, and a valid counter argument would be that you make absolutely sure to always store the timestamp unsigned, that it doesn't need to work until 2106 because it's a toy project, or that an overflow wouldn't really matter because you make sure to keep it unique even after an overflow, but I get scared any time storing a UNIX timestamp in 32 bits is mentioned, and am wondering if you've considered it.
def epoched_random_uid, do: epoched_random_uid(&:erlang.system_time/1, &:crypto.strong_rand_bytes/1)
def epoched_random_uid(system_time, strong_rand_bytes) do
time = system_time.(:second)
randint = strong_rand_bytes.(4) |> :crypto.bytes_to_integer
bor(time <<< 31, randint >>> 1)
end
So it uses – or at least I hope it does; you may file a bug report – a full 32 bits for the time, but it leaves the sign bit to be zero. I didn't want to deal with negative uids in PostgreSQL (bigint), but they actually would be perfectly valid. The sign bit could be used to expand the timestamp another bit.You could argue that the "2038 problem" (31 effective bits) in the context of a uid scheme of an application that is bootstrapped today, is actually the "2084 problem", because the wraparound to timestamps in 1970 could be construed as being from 2018.
You're scared because an average app hasn't considered a problem that's 21 years off? Seems like a premature worry
'Those who cannot remember the past are condemned to repeat it.'. --George Santayana
module.exports = function (size) {
return random(size || 22)
.map(r => url[r & 63])
.join('');
};I am always disappointed to see these kind of packages, as mentioned somewhere in comments above
> It is noticable again and again that devs publishing to npm are not up to snuff, I wish I knew why, as this does not happen with other dynamic languages/their code repos.
And I totally agree!
var masks = [15, 31, 63, 127, 255]
...
var mask = masks.find(function (i) {
return i >= alphabet.length - 1
})
...
var byte = bytes[i] & mask
if (alphabet[byte]) {
id += alphabet[byte]
Masking results in a random uniform number that is a power of two and greater or equal than the alphabet length and then indexes that are greater than alphabet length are rejected, which is a common way of avoiding modulo bias.I am always disappointed to see these kind of packages, as mentioned somewhere in comments above
Because you didn't understand how it worked and assumed that it's bad?
var bytes = random(size)
for (var i = 0; i < size; i++) {
id += url[bytes[i] & 63]
}
This part of the code just uses an alphabet size of 64, which makes the approach actually used in the exported function identical to modding the byte by the alphabet size.https://github.com/AltusAero/bronze
It's designed for use cases where collision-resistance (both time-based [UUID1] and random [UUID4]) is valued more than the size of the id. Can be used as a module (Node.js & browser) and via CLI.
https://github.com/skorokithakis/shortuuid
That way, elements are neither enumerable nor guessable.
Oh, and by the way, fuck HN for deleting comments.
https://github.com/dylang/shortid/blob/master/lib/random/ran...
But since LCGs are easy to solve, after seeing a few values you can solve for the seed and generate all past and future values.
But you don't even need to do anything fancy for that. Since their implementation of LCG only has a state size of 233280 different values, you can just brute force it. (also means that their rng could only ever generate 233280 different numbers to begin with)
Why the fuck do they have 2.5k stars!?
EDIT: They already have an open issue for it but project seems unmaintained: https://github.com/dylang/shortid/issues/70. Stay far far away.