globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

Hash collision for runtime keys (bug) #609

Open igalmizrahi opened 8 years ago

igalmizrahi commented 8 years ago

Looking a bit into how you generate the runtime keys for production builds, I noticed you are using a hash function (relevant files: runtime-key.js and hash.js). This issue probably doesn't occur often; but I'm wondering what is the correct behavior when there is a collision between generated keys, i.e. the hash function generates the same thing for distinct objects.

So I've written a sample test case with message keys "Z@S.ME" and "Z@RN.E" (these keys will collide according to http://stackoverflow.com/a/6120672). The end result is only one of the strings is in the compiled bundle. Here is my my test case:

var Globalize = require("globalize");
Globalize.load(require("cldr-data").entireSupplemental());
Globalize.load(require("cldr-data").entireMainFor("pt"));
Globalize.loadMessages({
    pt: {
        "Z@S.ME": "my first string",
        "Z@RN.E": "my second string"
    }
});
var GlobalizeCompiler = require("globalize-compiler");
var fs = require('fs');
fs.writeFile("output.js", GlobalizeCompiler.compile([
    Globalize("pt").messageFormatter("Z@S.ME"),
    Globalize("pt").messageFormatter("Z@RN.E"),
]));

In the output there's only one message function with key a1793498274, that contains the first string. Obviously at this point console.log(Globalize("pt").messageFormatter("Z@RN.E")()) also gives the incorrect string.

rxaviers commented 8 years ago

Currently, there's no hash collision handling and we should definitely have one.