Skip to content

Instantly share code, notes, and snippets.

View Alexzanderk's full-sized avatar
:octocat:

Alexander Kotsarev Alexzanderk

:octocat:
View GitHub Profile
@Alexzanderk
Alexzanderk / launch.json
Created June 12, 2020 12:08
Typescript Nodejs debugger launch
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
// Nodejs encryption with GCM
// Does not work with nodejs v0.10.31
// Part of https://github.com/chris-rock/node-crypto-examples
var crypto = require('crypto'),
algorithm = 'aes-256-gcm',
password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY',
// do not use a global iv for production,
// generate a new one for each encryption
iv = '60iP0h6vJoEa'
@Alexzanderk
Alexzanderk / typescriptClusterNodeJs.ts
Created July 12, 2020 19:00
typescript cluster NodeJs
import cluster from "cluster";
import { cpus } from "os";
const NUM_WORKERS = cpus().length;
const PATH_TO_SERVER_APP = __dirname + '/app.ts';
cluster.setupMaster({
execArgv: ['-r', 'tsconfig-paths/register', '-r', 'ts-node/register'],
exec: PATH_TO_SERVER_APP
} as cluster.ClusterSettings)
@Alexzanderk
Alexzanderk / app.get.js
Last active June 6, 2021 14:47 — forked from BetterProgramming/app.get.js
Video online
app.get('/video', function(req, res) {
const path = 'assets/sample.mp4'
const stat = fs.statSync(path)
const fileSize = stat.size
const range = req.headers.range
if (range) {
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10)
const end = parts[1]
? parseInt(parts[1], 10)
@Alexzanderk
Alexzanderk / launch.json
Created July 28, 2020 20:26 — forked from joelharkes/launch.json
Debug ts-mocha in vs code (visual studio code)
{
"version": "0.2.0",
"configurations": [
{
"name": "Run ts-mocha File",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-mocha",
"runtimeArgs": [
"${file}"
@Alexzanderk
Alexzanderk / arrayAutofocus.tsx
Created October 30, 2020 12:48
React autoFocus useRef
const useFocus = (count: number) => {
const refsArr: Array<React.RefObject<HTMLInputElement>> = [];
for (let index = 0; index < count; index++) {
const htmlElRef: React.RefObject<HTMLInputElement> = useRef(null);
refsArr[index] = htmlElRef;
}
const setFocus = (index: number): void => {
refsArr[index].current && refsArr[index].current.focus();
@Alexzanderk
Alexzanderk / generateSertificate.sh
Created December 9, 2020 08:44
Generate sertificate
openssl req -new -x509 -nodes -newkey rsa:1024 -keyout server.key -out server.crt
@Alexzanderk
Alexzanderk / repositoryService.ts
Created March 1, 2021 13:56
sequelize generic repository service
type Constructor<T> = new (...args: any[]) => T;
type ModelType<T extends Model<T>> = Constructor<T> & typeof Model;
export interface IRepository<T extends Model> {
get(id: string): Promise<T| null>;
find(where: FindOptions<T>): Promise<T>;
create(model: T): Promise<T>;
update(key: any, model: T): Promise<T>;
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
var nextChar = c=>c?String.fromCharCode(c.charCodeAt(0)+1):'A';
var nextCol = s=>s.replace(/([^Z]?)(Z*)$/, (_,a,z)=>nextChar(a) + z.replace(/Z/g,'A'));
//test:
nextCol(''); //A
nextCol('A'); //B
nextCol('Z'); //AA
nextCol('AA'); //AB
nextCol('XYZ'); //XZA