Add fallback when uuid is not available

If the `uuid` module does not exist, fall back to a primitive ID
generation mechanism based on just math.random().  The fallback
mechanism provides the same amount of entropy so we can be sure this
won't degrade quality of the identifiers.
dev
rahix 5 years ago
parent e2da98fbf4
commit 22328f313d

@ -1,4 +1,3 @@
local uuid = require("uuid")
local bit32 = require("bit32") local bit32 = require("bit32")
local Version = {0, 1} local Version = {0, 1}
@ -40,6 +39,23 @@ do
clock = os.clock clock = os.clock
end end
end end
local next_uuid -- Generate a UUID
do
local status, uuid = pcall(require, "uuid")
if status and uuid.next ~= nil then
next_uuid = uuid.next
else
-- Fallback to something reasonably close
next_uuid = function()
local uuid = ""
for _ = 1,16 do
uuid = uuid..string.format("%02x", math.random(0, 255))
end
return uuid
end
end
end
-- }}} -- }}}
-- Network ---------------------------------------------------------------- {{{ -- Network ---------------------------------------------------------------- {{{
@ -420,7 +436,7 @@ function BaseControl:listen(noun, query, callback)
if self.listeners[noun] == nil then if self.listeners[noun] == nil then
self.listeners[noun] = {} self.listeners[noun] = {}
end end
local id = uuid.next() local id = next_uuid()
self.listeners[noun][id] = { self.listeners[noun][id] = {
query=query, query=query,
callback=callback, callback=callback,

Loading…
Cancel
Save