Skip to content

Instantly share code, notes, and snippets.

View julik's full-sized avatar
💭
🎺

Julik Tarkhanov julik

💭
🎺
View GitHub Profile
class MultipartRangesWriter
ALPHABET = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
def initialize
# RFC1521 says that a boundary "must be no longer than 70 characters, not counting the two leading hyphens".
# Modulo-based random is biased but it doesn't matter much for us
@boundary = SecureRandom.bytes(64).unpack("C*").map {|b| ALPHABET[b % ALPHABET.length] }.join
end
class NullWriter < Struct.new(:offset)
@julik
julik / csrf_adapter.rb
Created August 18, 2019 15:37
Minimum viable reuse of the Rails CSRF token in Rack
# Allows a Rack application to reuse the Rails-provided CSRF
# protection, with the same guarantees and the same token.
# Implements token unmasking and other facilties.
class Middleware::CSRFAdapter
AUTHENTICITY_TOKEN_LENGTH = 32
class InvalidOrMissingToken < StandardError
def http_status_code
403
end
@julik
julik / csrf_adapter.rb
Created August 18, 2019 15:37
Minimum viable reuse of the Rails CSRF token in Rack
# Allows a Rack application to reuse the Rails-provided CSRF
# protection, with the same guarantees and the same token.
# Implements token unmasking and other facilties.
class Middleware::CSRFAdapter
AUTHENTICITY_TOKEN_LENGTH = 32
class InvalidOrMissingToken < StandardError
def http_status_code
403
end
@julik
julik / csrf_adapter.rb
Created August 18, 2019 15:37
Minimum viable reuse of the Rails CSRF token in Rack
# Allows a Rack application to reuse the Rails-provided CSRF
# protection, with the same guarantees and the same token.
# Implements token unmasking and other facilties.
class Middleware::CSRFAdapter
AUTHENTICITY_TOKEN_LENGTH = 32
class InvalidOrMissingToken < StandardError
def http_status_code
403
end
@julik
julik / eventuality.rb
Last active June 24, 2019 23:13
Visualser for orderings of given events. When your jobs can execute concurrently and you are faced with weirdo concurrency bugs in time, this might help visualise the variants of execution order. Especially when multiple tasks might be executing at the same time but actually depend on each other's output, and your execution engine cannot enforce…
require 'set'
class Eventuality
def initialize
@events = Set.new
@invariants = []
@tasks = []
end
def add_single_event(evt)
// Find at which index an element should be inserted
// into a sorted array, using binary search
function insertionIndexUsingBinarySearch(sortedArray, value, from, to) {
if (sortedArray.length === 0) {
return 0;
}
if (typeof(from) === 'undefined') {
from = 0;
}
@julik
julik / moocom_inner_layer_swatches.jsx
Created April 26, 2019 19:43
moo.com Illustrator colors (use "File -> Run Script" to create swatches)
function addCMYKSwatch(name, c, m, y, k) {
var myDoc = app.activeDocument;
var mySwatch = myDoc.swatches.add();
var color = new CMYKColor();
color.cyan = c;
color.magenta = m;
color.yellow = y;
color.black = k;
mySwatch.color = color;
mySwatch.name = name;
@julik
julik / mrs.go
Last active January 27, 2019 23:36
Go MultiReadSeeker (for stringing together multuple ReadSeeker structs - for example for edge includes using ServeContent)
package mrs
import "io"
type multiReadSeeker struct {
Readers []io.ReadSeeker
Pos int64
BytesTotal int64
BytesRead int64
locations []location
@julik
julik / ring.rb
Last active January 15, 2019 20:32
A small ring buffer implementation in Ruby
# A ring buffer enumerable object
class Ring
include Enumerable
def initialize(size)
@array = Array.new(size)
@pos = 0
end
def pos
@julik
julik / watchist.rb
Last active August 31, 2017 13:41
For the guard-tired
require 'thread'
# I am very Watchist. Use me in your config.ru like so:
# Watchist.start_and_mount_in_rack(self)
class Watchist
CHMUX = Mutex.new
$watchist_channels = []
$watchist_logger = Logger.new($stderr)
$watchist_logger.progname = "watchist"