Skip to content

Instantly share code, notes, and snippets.

@avimar
avimar / index.js
Last active December 17, 2015 08:49
Restify with optional HTTPS redirect
//This allows you to specify a certificate and a key and will create a server that just handles redirects, if you want it.
/*Using let's encrypt to create a free certificate:
cd /root/
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto certonly --webroot -w /my/web/root --email my@email.com -d myDomain.com
//add ssl:certificate location of 'fullchain.pem' and ssl:key location of 'privkey.pem'
*/
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
var myURL="mydomain.com";
var restifyOptions={}
restifyOptions.certificate = fs.readFileSync('PathTo:fullchain.pem');
restifyOptions.key = fs.readFileSync('PathTo:privkey.pem');
var server = restify.createServer(restifyOptions);
var nonSecure = restify.createServer({name: 'redirectToSSL'});
nonSecure.get(/.*/,function (req, res, next) {
res.redirect(301, 'https://' + myURL + req._url.href, next);
//using a function-scope `data` variable to save the data
function getData(respondentId) {
var data={};
return Respondent.findById(respondentId)
.then(respondent => {
data.respondent = respondent;
return Box.findById(respondent.box_id)
})
.then(box => {
data.box = box
@avimar
avimar / file.js
Created July 13, 2016 17:55
append a value to the query string
function checkAndSetAccount(){
var q = riot.route.query();
if (Cookies.get('account')==q.account) return;//already done.
if (q.account) {
Cookies.set('account',q.account);
auth.trigger('switchAccount');
}
if (!q.account && Cookies.get('account')!==Cookies.get('accountOriginal')) {//nothing in query string: need to set it
//console.log(window.location)
var newLocation=window.location.hash;
var _putItem = function (item, shouldCreateNewItem, shouldReturnInformationFromJoinedTable) {
return knex(...).update()
.then(function(affectedRows) {
if (affectedRows == 0 && shouldCreateNewItem) return knex.insert(...);
else if (shouldReturnInformationFromJoinedTable) return knex(...). ....leftJoin(..).select().first()
else return true;
})
}
@avimar
avimar / knex-functions.js
Created August 28, 2016 07:15
knex mysql update on duplicate and replace
function upsert(table, data, update){
if(!update) update=data;
var insert = knex(table).insert(data).toString();
var update = knex(table).update(update).toString().replace(/^update .* set /i, '');
return knex.raw(insert + ' on duplicate key update ' + update);
}
function replace(table, data){
var insert = knex(table).insert(data).toString().replace(/^INSERT/i, 'REPLACE');
return knex.raw(insert);
@avimar
avimar / getRegion.js
Last active September 15, 2016 04:20
north-america-area-codes
var NorthAmerica=require('./NorthAmerica.json');
var fp = require('lodash/fp');
function getRegionForAreaCode(areaCode){
return fp.findKey(function(o) {
return o.includes(areaCode);
})(NorthAmerica);
}
@avimar
avimar / pcap2wav
Last active March 1, 2023 22:32
Convert raw PCAP files into a .wav file
#!/bin/bash
#
# pcap2wav
# Original Author: Michael Collins <msc@freeswitch.org>
#Standard disclaimer: batteries not included, your mileage may vary...
# Updated by Avi Marcus <avi@bestfone.com>
#
# Accepts arg of pcap file w/only 2 RTP streams
# Creates a .<codec> file and a .wav file
# For codecs other than PCMA and PCMU the script calls fs_cli and does a little recording to create the wav file(s)
@avimar
avimar / isracard.js
Last active June 18, 2020 10:04 — forked from etodanik/isracard.js
Checks the validity of a given Isracard credit card number
/*License: MIT*/
function isracardCheck(num) {//algorithm explanation: https://web.archive.org/web/20140227235803/http://povolotski.me/2013/09/24/isracard-credit-card-number-validation-2/
if(typeof num !== 'number') num=''+num;
if(num.length < 8 || num.length > 9) return false;
var sum=0;
num.split('').forEach(function(val,key){
sum+=parseInt(val,10)*(num.length-key);
})
return sum % 11 == 0;
}