├── CHANGES.txt ├── LICENSE ├── README.md ├── lib └── resty │ └── uuid.lua └── lua-resty-uuid-dev-1.rockspec /CHANGES.txt: -------------------------------------------------------------------------------- 1 | 2 | Changes with lua-resty-uuid 1.1 13 Apr 2016 3 | 4 | *) Bugfix: Fixes issue with library loader on Linux. 5 | (See also: https://github.com/bungle/lua-resty-uuid/issues/3) 6 | 7 | Changes with lua-resty-uuid 1.0 22 Jan 2016 8 | 9 | *) Feature: LuaRocks Support via MoonRocks. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 – 2016 Aapo Talvensaari 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | lua-resty-uuid 2 | ============== 3 | 4 | LuaJIT FFI bindings for libuuid, a DCE compatible Universally Unique Identifier library. 5 | -------------------------------------------------------------------------------- /lib/resty/uuid.lua: -------------------------------------------------------------------------------- 1 | local ffi = require "ffi" 2 | local ffi_new = ffi.new 3 | local ffi_str = ffi.string 4 | local ffi_load = ffi.load 5 | local ffi_cdef = ffi.cdef 6 | local C = ffi.C 7 | local OSX = ffi.os == "OSX" 8 | local pcall = pcall 9 | local assert = assert 10 | local tonumber = tonumber 11 | local setmetatable = setmetatable 12 | 13 | ffi_cdef[[ 14 | typedef unsigned char uuid_t[16]; 15 | typedef long time_t; 16 | typedef struct timeval { 17 | time_t tv_sec; 18 | time_t tv_usec; 19 | } timeval; 20 | void uuid_generate(uuid_t out); 21 | void uuid_generate_random(uuid_t out); 22 | void uuid_generate_time(uuid_t out); 23 | int uuid_generate_time_safe(uuid_t out); 24 | int uuid_parse(const char *in, uuid_t uu); 25 | void uuid_unparse(const uuid_t uu, char *out); 26 | int uuid_type(const uuid_t uu); 27 | int uuid_variant(const uuid_t uu); 28 | time_t uuid_time(const uuid_t uu, struct timeval *ret_tv); 29 | ]] 30 | 31 | local function L(n) 32 | local ok, lib = pcall(ffi_load, n) 33 | if ok then return lib end 34 | ok, lib = pcall(ffi_load, n .. '.so.1') 35 | assert(ok, lib) 36 | return lib 37 | end 38 | 39 | local lib = OSX and C or L "uuid" 40 | local uid = ffi_new "uuid_t" 41 | local tvl = ffi_new "timeval" 42 | local buf = ffi_new("char[?]", 36) 43 | 44 | local uuid = {} 45 | local mt = {} 46 | 47 | local function unparse(id) 48 | lib.uuid_unparse(id, buf) 49 | return ffi_str(buf, 36) 50 | end 51 | 52 | local function parse(id) 53 | return lib.uuid_parse(id, uid) == 0 and uid or nil 54 | end 55 | 56 | function uuid.generate() 57 | lib.uuid_generate(uid) 58 | return unparse(uid) 59 | end 60 | 61 | function uuid.generate_random() 62 | lib.uuid_generate_random(uid) 63 | return unparse(uid) 64 | end 65 | 66 | function uuid.generate_time() 67 | lib.uuid_generate_time(uid) 68 | return unparse(uid) 69 | end 70 | 71 | function uuid.generate_time_safe() 72 | assert(not OSX, "uuid_generate_time_safe is not supported on OS X.") 73 | local safe = lib.uuid_generate_time_safe(uid) == 0 74 | return unparse(uid), safe 75 | end 76 | 77 | function uuid.type(id) 78 | assert(not OSX, "uuid_type is not supported on OS X.") 79 | local parsed = parse(id) 80 | return parsed and lib.uuid_type(parsed) 81 | end 82 | 83 | function uuid.variant(id) 84 | assert(not OSX, "uuid_variant is not supported on OS X.") 85 | local parsed = parse(id) 86 | return parsed and lib.uuid_variant(parsed) 87 | end 88 | 89 | function uuid.time(id) 90 | local parsed = parse(id) 91 | if parsed then 92 | local secs = lib.uuid_time(parsed, tvl) 93 | return tonumber(secs), tonumber(tvl.tv_usec) 94 | end 95 | end 96 | 97 | function uuid.is_valid(id) 98 | return not not parse(id) 99 | end 100 | 101 | mt.__call = uuid.generate 102 | 103 | return setmetatable(uuid, mt) -------------------------------------------------------------------------------- /lua-resty-uuid-dev-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-uuid" 2 | version = "dev-1" 3 | source = { 4 | url = "git://github.com/bungle/lua-resty-uuid.git" 5 | } 6 | description = { 7 | summary = "DCE compatible Universally Unique Identifier (UUID/GUID) library", 8 | detailed = "lua-resty-uuid is a DCE compatible Universally Unique Identifier library for Lua and OpenResty.", 9 | homepage = "https://github.com/bungle/lua-resty-uuid", 10 | maintainer = "Aapo Talvensaari ", 11 | license = "BSD" 12 | } 13 | dependencies = { 14 | "lua >= 5.1" 15 | } 16 | build = { 17 | type = "builtin", 18 | modules = { 19 | ["resty.uuid"] = "lib/resty/uuid.lua" 20 | } 21 | } 22 | --------------------------------------------------------------------------------