Skip to content

Instantly share code, notes, and snippets.

View daurnimator's full-sized avatar

daurnimator

View GitHub Profile
@daurnimator
daurnimator / webrtc.js
Last active November 7, 2023 07:10
Playing with WebAudio and WebRTC
// Browser compat
window.AudioContext = window.AudioContext || window.webkitAudioContext;
window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
var key = window.location.hash.substring(1);
var audio_context = new AudioContext();
var local_output = audio_context.createMediaStreamDestination();
@daurnimator
daurnimator / 4CF.c
Last active August 4, 2023 00:22
Use your own main loop on OSX. Related blog post: http://daurnimator.com/post/147024385399/using-your-own-main-loop-on-osx
#include <mach/port.h> /* mach_port_t */
#include <mach/mach.h> /* mach_port_allocate(), mach_task_self(), mach_port_insert_member(), MACH_PORT_RIGHT_PORT_SET */
#include <sys/event.h> /* kqueue(), kevent64(), struct kevent64_s, EVFILT_MACHPORT, EV_SET64, EV_ADD */
#include <sys/time.h> /* struct timespec */
//#include <dispatch/private.h>
extern mach_port_t _dispatch_get_main_queue_port_4CF(void);
extern void _dispatch_main_queue_callback_4CF(void);
#include <stdio.h>
@daurnimator
daurnimator / tarantool_cqueues.lua
Last active February 20, 2023 05:56
Tarantool + cqueues for lua-http
-- This code monkey patches cqueues primitives to allow for transparent use of cqueues inside of tarantool
local cqueues = require "cqueues"
local fiber = require "fiber"
local socket = require "socket" -- https://tarantool.org/en/doc/1.7/reference/reference_lua/socket.html (not luasocket)
local old_step; old_step = cqueues.interpose("step", function(self, timeout)
if cqueues.running() then
fiber.yield()
return old_step(self, timeout)
@daurnimator
daurnimator / rock_to_PKGBUILD.lua
Last active October 8, 2022 15:51
Script to convert LuaRocks to Arch Linux PKGBUILDs
#!/usr/bin/env lua
local rockspec_name = assert(arg[1])
local function log(level, fmt, ...)
io.stderr:write(level .. "\t" .. string.format(fmt, ...) .. "\n")
end
local function append(tbl, ...)
for _, v in ipairs {...} do
@daurnimator
daurnimator / ieee754.lua
Last active July 2, 2022 15:32
Reading IEEE-754 floating point numbers in Lua
--[[
A short module to read binary floating point numbers for lua.
]]
local has_ffi , ffi = pcall ( require , "ffi" )
local has_bit , bit = pcall ( require , "bit" )
local read_float , read_double
if has_ffi and has_bit then
local cast = ffi.cast
local DIRSEP = package.config:sub(1, 1)
local function is_readable(filename)
local fd = io.open(filename, "r")
if fd then
fd:close()
return true
else
return false
end
@daurnimator
daurnimator / echo_server.lua
Last active January 28, 2022 04:40
Example of lua-http server api
local port = arg[1] or "8000"
local nice_server = require "nice_server"
local timeout = 2
local function reply(resp)
local req_body = assert(resp.stream:get_body_as_string(timeout))
local req_body_type = resp.request_headers:get "content-type"
resp.headers:upsert(":status", "200")
@daurnimator
daurnimator / choose_type.lua
Created August 26, 2012 14:36
Lpeg pattern to parse a HTTP Accept Header
do
local function helper ( allowed , want )
return ( not want.type or allowed.type == "*" or ( allowed.type == want.type and
( not want.subtype or allowed.subtype == "*" or allowed.subtype == want.subtype ) )
) and allowed.q ~= 0
end
function choose_type ( allowed_list , wanted_list )
-- Remove items from allowed list that have no match (retains client ordering)
local res = { }
local allowed_wanted_map = { }
@daurnimator
daurnimator / dirmngr.conf
Last active July 14, 2021 08:24
My GPG config
hkp-cacert ~/.gnupg/sks-keyservers.netCA.pem
@daurnimator
daurnimator / fengari-vue.lua
Last active June 17, 2021 11:43
Playing with Vue from fengari
-- Load Vue library
package.loadlib("https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js", "*")
-- Get Object helper
local Object = dofile("https://gist.githubusercontent.com/daurnimator/5a7fa933e96e14333962093322e0ff95/raw/8c6968be0111c7becc485a692162ad100e87d9c7/Object.lua").Object
local myapp = js.new(js.global.Vue, Object{
el = "#foo";
template = [[
<div id="foo">{{message}}</div>
]];