Skip to content

Instantly share code, notes, and snippets.

View rla's full-sized avatar

Raivo Laanemets rla

View GitHub Profile
@rla
rla / formatter.js
Created August 8, 2017 16:25
Node-mysql custom formatter for values, identifiers and sort order
// Custom bound parameters.
// :something for values and ?something for identifiers.
// https://github.com/mysqljs/mysql#custom-format
const DIRECTIONS = ['ASC', 'DESC'];
module.exports = function(query, values) {
if (!values) {
return query;
}
141M node_modules/
26M node_modules/typescript
8.9M node_modules/caniuse-db
7.4M node_modules/rxjs
6.9M node_modules/core-js
5.7M node_modules/node-sass
5.3M node_modules/webpack
5.1M node_modules/less
4.9M node_modules/lodash
2.4M node_modules/har-validator
Local<Object> TermQuery(Local<Value> termString) {
Local<Object> variables = Nan::New<Object>();
String::Utf8Value string(termString);
term_t t = PL_new_term_ref();
if (!PL_chars_to_term(*string, t)) {
Nan::ThrowError("PL_chars_to_term failed.");
return variables;
}
functor_t f;
if (!PL_get_functor(t, &f)) {
@rla
rla / code.js
Created April 5, 2017 16:40
Queens ported to yield and generators
// http://www.javaist.com/blog/2008/11/06/eight-queens-problem-in-prolog/
const EMPTY_LIST = [];
function* between(start, end) {
for (let v = start; v < end; v++) {
yield v;
}
}
@rla
rla / code.js
Created March 27, 2017 13:52
Debounce
const assert = require('assert');
// Helper to debounce function calls.
module.exports = (time, fn) => {
assert.equal(typeof time, 'number');
assert.equal(typeof fn, 'function');
let debounce = false;
return function() {
if (debounce) {
@rla
rla / code.js
Created March 18, 2017 16:19
Middleware to redirect to HTTPS.
// Middleware to redirect to HTTPS.
module.exports = () => {
return (req, res, next) => {
if (req.protocol !== 'https') {
const host = req.hostname;
if (host && !req.url.match(/\.well-known/)) {
res.redirect(301, `https://${host}${req.url}`);
} else {
next();
server {
location ~ ^/something(.*)$ {
return 301 https://someplace.com;
}
return 301 https://example.com;
}
@rla
rla / instructions.md
Created December 17, 2016 16:07
Node 7.x with async/await without mem leaks

Node 7.x with async/await without mem leaks

Reference: nodejs/node#9618

git clone https://github.com/targos/node.git node-async-await
cd node-async-await
git checkout v8-5.5
./configure
make -j4
$ optipng download.png
** Processing: download.png
658x318 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 8 bits/pixel, 113 colors (112 transparent) in palette
Input IDAT size = 11654 bytes
Input file size = 11723 bytes
Trying:
zc = 9 zm = 8 zs = 0 f = 0 IDAT size = 3451
zc = 9 zm = 8 zs = 1 f = 0 IDAT size = 3419
@rla
rla / code.js
Created December 7, 2016 12:12
User.pre('save', wrap(async () => {
var user = this;
if (!user.isModified('password')) return;
var salt = await bcrypt.genSalt(10);
var hash = await bcrypts.hash(user.password, salt, null);
user.password = hash;
}));
const wrap = (fn) => {
return (next) => {