Skip to content

Instantly share code, notes, and snippets.

View le0pard's full-sized avatar
:octocat:
Simplicity is the soul of efficiency

Oleksii Vasyliev le0pard

:octocat:
Simplicity is the soul of efficiency
View GitHub Profile
@le0pard
le0pard / function.sql
Created April 6, 2023 16:14
Filtered Table Estimates
CREATE FUNCTION count_estimate(query text) RETURNS integer AS $$
DECLARE
rec record;
rows integer;
BEGIN
FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
rows := substring(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
EXIT WHEN rows IS NOT NULL;
END LOOP;
RETURN rows;
create function lcfirst(word text)
returns text
language plpgsql
immutable
as $$
begin
return lower(left(word, 1)) || right(word, -1);
end;
$$;
@le0pard
le0pard / hmac256.js
Created September 14, 2022 12:22
Hmac sha-256 hexadecimal on webcrypto
const ALGORITHM = {
name: 'HMAC',
hash: 'SHA-256'
}
const bufferToHex = (buffer) => (
[...new Uint8Array(buffer)]
.map(b => b.toString(16).padStart(2, '0'))
.join('')
)
@le0pard
le0pard / b64utf.js
Created October 11, 2021 12:55
Base64 in browser with Unicode support (no deprecated 'unescape' and 'escape' functions)
const b64EncodeUnicode = (str) => (
window.btoa(window.encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_match, p1) => (
String.fromCharCode(`0x${p1}`)
)))
)
const b64DecodeUnicode = (str) => (
window.decodeURIComponent(window.atob(str).split('').map((c) => (
`%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`
)).join(''))
@le0pard
le0pard / bench.rb
Created September 10, 2021 12:15
OpenStruct vs Struct vs Hash in Ruby
require 'benchmark/ips'
require 'ostruct'
# Enable and start GC before each job run. Disable GC afterwards.
class GCSuite
def warming(*)
run_gc
end
@le0pard
le0pard / test.rb
Created September 2, 2021 15:47
unlogged tables in rails for test env
# unlogged tables used to speedup tests, but its are not crash proof
config.to_prepare do
ActiveSupport.on_load(:active_record) do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true
end
end
@le0pard
le0pard / forge-library.js
Last active December 28, 2020 20:07
AES-256-GCM on Ruby and JS (Web Crypto API and forge.js)
// you need to use https://github.com/digitalbazaar/forge
const encryptData = (key, data) => {
const mdKey = forge.md.sha256.create()
mdKey.update(key)
const iv = forge.random.getBytesSync(12)
const cipher = forge.cipher.createCipher('AES-GCM', mdKey.digest())
cipher.start({
@le0pard
le0pard / dns_example.rb
Created October 1, 2020 13:59
Proxy DNS with debug
# frozen_string_literal: true
require 'socket'
require 'resolv'
class DNSServer
attr_reader :port, :ttl
def initialize(port: 5300, ttl: 60, records: {})
@le0pard
le0pard / dns.rb
Created October 1, 2020 11:21
Simple Ruby DNS server
# frozen_string_literal: true
require 'socket'
require 'bindata'
class DnsHeader < BinData::Record
endian :big
uint16 :transaction_id
bit1 :flag_QR
@le0pard
le0pard / app.jsx
Created October 9, 2016 19:56
React and Flux example for screencast
import React from 'react'
import {Dispatcher} from 'flux'
import {EventEmitter} from 'events'
import keyMirror from 'fbjs/lib/keyMirror'
import './app.sass'
const actions = keyMirror({
PLUS_ACTION: null,
MINUS_ACTION: null