Skip to content

Instantly share code, notes, and snippets.

@mixdev
mixdev / README.md
Created December 22, 2022 01:53 — forked from babldev/README.md
NextJS Background Task

NextJS Background Task Example

An example of how to add a background task (e.g. a queue consumer) to an existing NextJS project.

Setup

  • Add your background task file
  • Add a new worker-tsconfig.json, specifically specifying "module": "commonjs" and targeting only the worker source files.
  • Add convenience functions for building & running to package.json

Then to build once:

//file: ./pages/api/checkHTTPMethod.ts
/* this code will allow only GET method requests on this route */
import { Middleware, use } from 'next-api-route-middleware';
import type { NextApiRequest, NextApiResponse } from 'next';
export const allowMethods = (allowedMethods: string[]): Middleware => {
return async function (req, res, next) {
@mixdev
mixdev / static_normalizer.js
Created September 6, 2019 17:32
A reusable rank normalizer. Should use with map. (Reminder. you may use a normal loop counter if you have just one place to use this)
class rankNormalizer{
static normalize(){
rankNormalizer.count = typeof rankNormalizer.count !== 'undefined'?++rankNormalizer.count:1
//console.log(rankNormalizer.count);
return rankNormalizer.count;
};
static reset(){
rankNormalizer.count=null
}
}
@mixdev
mixdev / walletaddress_from_publickey.js
Created June 6, 2017 14:30
How to recreate a Bitcoin wallet address from a public key
/*
How to recreate a Bitcoin wallet address from a public key
Ref: https://github.com/bitcoinjs/bitcoinjs-lib/issues/590
*/
var bitcoin = require('bitcoinjs-lib')
var assert = require('assert')
var bigi = require('bigi')
var crypto = require('crypto')
Signal.trap("SIGINT") do # SIGINT = control-C
exit
end
str =''
system("say 'Hi, whats your name?'")
sleep(0.5)
loop do
begin
@mixdev
mixdev / rolling_2dice_montecarlo.php
Last active April 29, 2017 17:19
Simple Monte Carlo Simulation of rolling two regular dice. Shows visual results in shell. Shows clear central tendency. The problem stated here http://www.goldsim.com/Web/Introduction/Probabilistic/MonteCarlo/
<?php
$simulations = 10000;
$dicesides = 6;
$count = Array();
for($i=0; $i<$simulations; $i++) {
$x = mt_rand(1,$dicesides);
$y = mt_rand(1,$dicesides);
@$count[$x+$y]++;
}
@mixdev
mixdev / blockid.txt
Created March 30, 2017 09:14
Verifying that "mixdev.id" is my Blockstack ID. https://onename.com/mixdev
Verifying that "mixdev.id" is my Blockstack ID. https://onename.com/mixdev
@mixdev
mixdev / couponcode_gen
Created September 8, 2013 19:26
Creates alpha numeric coupon codes with specified chars length. Not duplicates checks.
<html>
<head>
<title>Coupon generator</title>
</head>
<body bgcolor="white" text="black">
<script>
//var txt = 'aabcdefghijklmnopqrstuvwxyz0123456789';
var txt = 'aabcdefghjkmnpqrstuvwxyz23456789';//Remove indistinguishable chars like o,0,i,l,1