Skip to content

Instantly share code, notes, and snippets.

/* jshint node: true */
"use strict";
var Promise = require('bluebird');
var crypto = require('crypto');
var dgram = require('dgram');
var events = require('events');
var fs = require('fs');
var srp = require('srp');
var util = require('util');
'use strict';
var Transform = require('stream').Transform,
inherits = require('util').inherits;
var noBuf = new Buffer(0);
module.exports = LineStream;
function LineStream(opts) {
'use strict';
var Transform = require('stream').Transform,
inherits = require('util').inherits;
module.exports = ThrottleStream;
function ThrottleStream(opts) {
if (!(this instanceof ThrottleStream))
return new ThrottleStream(opts);
@myndzi
myndzi / gist:11299120
Created April 25, 2014 18:40
Get list of files in directory matching mask, excluding directories, with Bluebird
return Promise.promisify(fs.readdir)(config.path).filter(function (fileName) {
return config.mask.test(fileName);
}).map(function (fileName) {
var fullPath = path.join(config.path, fileName);
return Promise.promisify(fs.stat)(fullPath).tap(function (stat) {
stat.fullPath = fullPath;
stat.fileName = fileName;
});
}).filter(function (stat) {
return stat.isFile();
@myndzi
myndzi / gist:9026516
Last active August 29, 2015 13:56
React state update propagation helper
/* Call initially with arguments as a path from this.state
* to the key you want a callback to update:
*
* this will return a function to update this.state.foo:
* this.update('foo')
*
* and this will return one to update this.state[0].bar:
* this.update('state', 0, 'bar')
*
* All arguments are optional; this returns a callback
@myndzi
myndzi / gist:8985931
Last active August 29, 2015 13:56
Generically pass a config object into the body of an HTML document rendered by Dust
// in the app setup:
dust.helpers.config = function (chunk, ctx, bodies, params) {
var data = ctx.get('config');
if (!data) return chunk;
var str = JSON.stringify(data).replace("</script>", "<'+'/script>");
str = "<script>function CONFIG() { return JSON.parse('"+str+"'); }</script>";
return chunk.write(str);
};
@myndzi
myndzi / gist:8940959
Created February 11, 2014 18:28
Don't know if useful
'use strict';
var Promise = require('bluebird');
module.exports = getStreamData;
// return ascii character value from two ascii character values in hex representation
function fromHex(b1, b2) {
b1 -= 48; if (b1 > 9) { b1 -= 7; } // A -> 10
b2 -= 48; if (b2 > 9) { b2 -= 7; }
'use strict';
module.exports = function (app, opts) {
var fs = require('fs'),
path = require('path'),
join = path.join,
Promise = require('bluebird'),
extend = require('jquery-extend'),
dust = opts.helpers ?
require('dustjs-helpers') :
var Promise = require('bluebird'),
ie = require('int-encoder'),
crypto = require('crypto');
var alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()[{]}/?=+\\|-_\'",<.>;:';
function getRandomPass(len) {
var deferred = Promise.defer();
var bytes = Math.ceil(Math.log(alpha.length)/Math.log(256)*len);
var prompt = require('promised-prompt'),
Promise = prompt.Promise;
prompt({
required: true
}).ask('db type?', {
key: 'db',
type: 'multi',
default: 'pg',
validator: [ 'pg' ]