From 22328f313d72d9207a85e9f2b4b5ac9e974e43bd Mon Sep 17 00:00:00 2001 From: Rahix Date: Sat, 30 Jan 2021 11:10:11 +0100 Subject: [PATCH] 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. --- bc.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/bc.lua b/bc.lua index fa03de9dd7e1..bd4804501ad4 100644 --- a/bc.lua +++ b/bc.lua @@ -1,4 +1,3 @@ -local uuid = require("uuid") local bit32 = require("bit32") local Version = {0, 1} @@ -40,6 +39,23 @@ do clock = os.clock 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 ---------------------------------------------------------------- {{{ @@ -420,7 +436,7 @@ function BaseControl:listen(noun, query, callback) if self.listeners[noun] == nil then self.listeners[noun] = {} end - local id = uuid.next() + local id = next_uuid() self.listeners[noun][id] = { query=query, callback=callback,