Skip to content

Instantly share code, notes, and snippets.

View ArtemAvramenko's full-sized avatar

Artem ArtemAvramenko

View GitHub Profile
@ArtemAvramenko
ArtemAvramenko / x-headers-example.txt
Last active May 20, 2024 11:46
widely used private http x-headers
X-Frame-Options
X-Http-Method-Override
X-Csrf-Token
X-Real-IP
X-Forwarded-For
X-Forwarded-Host
X-Forwarded-Proto
X-Original-For
X-Original-Host
X-Original-Proto
@ArtemAvramenko
ArtemAvramenko / TemporaryQueue.cs
Created May 13, 2024 15:25
Queue/cache of items that are stored for a limited period of time
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace ArtemAvramenko.Collections;
/// <summary>
/// Queue/cache of items that are stored for a limited period of time.
/// </summary>
@ArtemAvramenko
ArtemAvramenko / read-text-from-file.js
Last active June 16, 2024 11:32
JavaScript code to read unicode text from a file on the browser side
function readTextFromFile(file, maxMegabytes = 1) {
const reader = new FileReader();
return new Promise(resolve => {
// check file size
if (!file || !file.size) {
resolve({ error: `The file cannot be empty` });
return;
}
if (file.size > maxMegabytes * 0x100000) {
@ArtemAvramenko
ArtemAvramenko / restore-indexes.sql
Last active March 29, 2024 11:11
The T/SQL script solves the problem with is_not_trusted indexes after moving data to another Azure db without using the sp_MSforeachtable proc
DECLARE
@Schema NVARCHAR(MAX),
@Name NVARCHAR(MAX),
@Sql NVARCHAR(MAX)
DECLARE TableCursor CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
OPEN TableCursor
WHILE 1 = 1
@ArtemAvramenko
ArtemAvramenko / basic-plural.js
Last active March 13, 2024 22:38
The simplest pluralizer according to English rules. It doesn't contain a list of exceptions, so it will return 'mouses' instead of 'mice', 'mans' instead of 'men', etc.
module.exports = function getPlural(name) {
const match = (r, n, e) => r.test(name) && (n ? name.slice(0, -n) : name) + e;
return match(/sis$/, 2, 'es') // analyses, diagnoses
|| match(/(?:[^fgmq][aeoi]|[fglm][eio]|[^aeijoqsuvxz])s$/, 0, '') // physics, stairs
|| match(/(?:s|x|z|[cs]h|ato)$/, 0, 'es') // kisses, tomatoes
|| match(/(?:[^aeiou]|qu)y$/, 1, 'ies') // ladies, tries
|| match(/[^ch][ieo][ln]ey$/, 2, 'ies') // smilies, monies
|| match(/(?:[aeou]l|[eo]a|ar)f$/, 1, 'ves') // scarves, shelves
|| match(/[^defr]ife$/, 2, 'ves') // knives, wives
|| name + 's';
@ArtemAvramenko
ArtemAvramenko / failure-probability.md
Last active April 3, 2024 20:08
Probability of failure of a complex system
Number of elements Reliability of every single element Overall probability of failure
100 99% >60%
1000 99.9% >60%
10000 99.99% >60%
Number of elements Reliability of every single element Overall probability of failure
100 99.99% <1%
@ArtemAvramenko
ArtemAvramenko / LargeBitArray.cs
Created November 1, 2023 13:36
BitArray implementation that allows to store more than 2^32 values
public class LargeBitArray
{
private readonly int _bucketSize;
private readonly long _capacity;
public LargeBitArray(long capacity = int.MaxValue) // ~272 MB for 2^31 values
{
if (capacity < 1 || capacity > (long)2e18)
{
throw new ArgumentException("Capacity is out of range");
@ArtemAvramenko
ArtemAvramenko / node-script-with-dependencies.js
Last active October 11, 2023 11:47
A standalone single-file Node.js script that installs dependencies locally in dotfiles
/**
* This small function allows you to distribute your script as a single file, without
* requiring a separate package.json. When running a script through a node, the dependencies
* that are installed will not affect neighboring scripts or global packages.
* @summary Installs dependencies in the local dotfiles folder
*/
const installDependencies = dependencies => {
const fs = require('fs');
const dependenciesPath = './.npm-' + __filename.match(/[^\\/]+$/)[0];
fs.existsSync(dependenciesPath) || fs.mkdirSync(dependenciesPath);
@ArtemAvramenko
ArtemAvramenko / AsyncLazy.cs
Last active October 5, 2023 11:53
Lazy pattern implementation with asynchronous locks that release the thread for waiting time
// https://stackoverflow.com/questions/17975609/lazy-load-properties-with-async/41141728#41141728
#nullable enable
public class AsyncLazy<T>
{
private Func<Task<T>>? _valueFactory;
private volatile SemaphoreSlim? _semaphore = new(1, 1);
private volatile Boxed? _boxed;
@ArtemAvramenko
ArtemAvramenko / dark-mode.reg
Created September 8, 2023 13:52
Enable the dark theme through the registry
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize]
"AppsUseLightTheme"=dword:00000000