Skip to content

Instantly share code, notes, and snippets.

@JackyYin
JackyYin / jwt.js
Created August 6, 2019 15:54
a JWT factory with blacklist and refresh function
const jwt = require('jsonwebtoken');
module.exports = (redisC) => {
const secret = process.env.JWT_SECRET;
const accessExpiresIn = 60 * 15;
const refreshExpiresIn = 60 * 60;
const sign = ({ payload = {}, options = {}, type = 'access' }) => {
payload = {...payload, type};
options = options.expiresIn ? options : {
@JackyYin
JackyYin / docker-compose.yml
Created December 23, 2019 07:34
docker-compose for local mongoDB
version: '3.5'
services:
mongo:
image: mongo
ports:
- '27017:27017'
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
@JackyYin
JackyYin / sort.c
Created December 14, 2020 03:39
5 sorting algorithems includes bubble sort, selection sort, insertion sort, merge sort and quick sort
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void _swap (int *arr, size_t i, size_t j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
@JackyYin
JackyYin / init.toml
Last active July 3, 2021 03:37
my spacevim config
#=============================================================================
# dark_powered.toml --- dark powered configuration example for SpaceVim
# Copyright (c) 2016-2020 Wang Shidong & Contributors
# Author: Wang Shidong < wsdjeg at 163.com >
# URL: https://spacevim.org
# License: GPLv3
#=============================================================================
# All SpaceVim option below [option] section
[options]
@JackyYin
JackyYin / myspacevim.vim
Last active July 3, 2021 03:39
some custom config for space vim
function! myspacevim#before() abort
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" BASICS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax on
set updatetime=1000
"for performance issue
set re=1
@JackyYin
JackyYin / rsa.js
Created January 16, 2021 12:58
test size limit of RSA message body
const bits = 2048
const { publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: bits,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
}
})
const test = (len) => {
@JackyYin
JackyYin / coroutine.c
Last active March 30, 2021 09:10
a simple coroutine implementation
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#define COROUTINE_START int r = setjmp(current->context);\
switch (r) {\
case 0:
#define COROUTINE_PREEMPT if (current->iter % 500 == 0) {\
longjmp(m, 1);\
@JackyYin
JackyYin / remove_bad_utf8.c
Last active September 3, 2021 09:04
in-place replace bad UTF-8 chars for CLD2
void remove_bad_utf8 (uint8_t *src, size_t srclen)
{
uint8_t *dst = src;
uint8_t *srcend = src + srclen;
while (src < srcend) {
// 0xxxxxxx
if ((src[0] & 0x80) == 0x00) {
if (src[0] < 0x20 || src[0] == 0x7F) {
/* printf("not printable one-byte char...%02X\n", src[0]); */
@JackyYin
JackyYin / coroutine_ucontext.c
Created September 12, 2021 14:34
a simple coroutine implementation using ucontext
#include <ucontext.h>
#include <stdio.h>
#include <stdlib.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define CO_STACK_SIZE 2048
typedef struct {
@JackyYin
JackyYin / futex.c
Last active September 24, 2021 08:00
A simple program using futex to mimic mutex, but much slower than mutex
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdatomic.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/futex.h>