Skip to content

Instantly share code, notes, and snippets.

View cerebrl's full-sized avatar

Justin Lowery cerebrl

View GitHub Profile
@cerebrl
cerebrl / factories-mixins-inheritence.js
Created August 2, 2015 03:17
Playing with factories, prototypes, behavior delegation, mixins and more: http://cerebrl.jsbin.com/zaxaqo/edit?js,console …. I love JavaScript!
/** ****************************************
* Utility file
* mixin.js
* @returns [object]
*/
function mixinFactory() {
var len = arguments.length,
i = 0,
finalObj = {};
@cerebrl
cerebrl / safeInterpolate.js
Created March 11, 2015 23:07
Simple Server-Side String Interpolation. Demo: http://jsbin.com/pipudujixo/1/edit. Requires safeGet() here: https://gist.github.com/cerebrl/a52b69aafa9bf820bb1e
/* Simple, Server-Side String Interpolation
* Base on John Resig's Microtemplating: http://ejohn.org/blog/javascript-micro-templating/
*
* @param str {String} A string with key or object path in handlebar-like syntax: e.g. {{user.name}}
* @param data {Object} The data source with with to grab the key matching values
* @return {String} The final string with key-value replacements
*/
var safeInterpolate = function safeInterpolate (str, data){
// Create a new function with the template converted into executable code.
var fn = new Function("obj",
@cerebrl
cerebrl / safeGet.js
Created February 12, 2015 21:41
Be able to safely access props on deep objects. Demo: http://jsbin.com/beferuviqo/1/edit
// Sample object
var mySampleData = {
foo: {
bar: [
1,
2,
{
baz: "hello",
wut: 2,
hi: [7, 8, 9]
@cerebrl
cerebrl / es6-features.md
Last active March 14, 2019 13:07
Interesting ES6 features
@cerebrl
cerebrl / factories-and-services-oop.md
Created November 25, 2013 07:18
Factories and Services in Angular

Factories, and services in angular are singleton by definition. If you want to create a new instance you could always just do something like:

module.factory('myIntanceCreator', function(){
      return {
           create: function(){
                // create your instance here
            }
      };
@cerebrl
cerebrl / q-io-body-issue.md
Created November 5, 2013 06:11
Q-IO HTTP Body Issue

I have two instances of Node.js running on my local environment, both with Express. One is the actual application client and the other is a mock API server, which is really nothing more than for me to ping against. The client has a basic model with CRUD object on it. Here's an example of the POST with the issue:

create: function (options) {

	return qHttp.request({
			"host": "127.0.0.1",
			"port": "8000"
			"method": "POST",
 "path": "/sessions",
@cerebrl
cerebrl / requirejs-troubleshooting.md
Created September 13, 2013 18:14
Some nice tricks for troubleshooting RequireJS modules: http://tech.pro/blog/1561/five-helpful-tips-when-using-requirejs

Some Tricks for Troubleshooting

You can use these API calls in your code if you need to, but I've actually found them quite useful when on the console in Chrome:

require.defined(moduleId) - returns true if your moduleId has been defined and is ready for use.

require.specified(moduleId) - returns true if your moduleId has been listed as a dependency by another defined module. Note that just because this returns true doesn't mean your moduleId is ready to use (don't you just love asynchrony?).

requirejs.s.contexts._.config - I learned about this from Vernon Kesner. This is technically a "back door/undocumented" call - so it could change or disappear without warning. However it returns a very useful object full of configuration info, see below: Chrome Console results for requrejs.s.conects._.config

@cerebrl
cerebrl / 1-securing-express.md
Last active August 2, 2023 22:48
Securing ExpressJS

tl;dr

  1. Don't run as root.
  2. For sessions, set httpOnly (and secure to true if running over SSL) when setting cookies.
  3. Use the Helmet for secure headers: https://github.com/evilpacket/helmet
  4. Enable csrf for preventing Cross-Site Request Forgery: http://expressjs.com/api.html#csrf
  5. Don't use the deprecated bodyParser() and only use multipart explicitly. To avoid multiparts vulnerability to 'temp file' bloat, use the defer property and pipe() the multipart upload stream to the intended destination.
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
// This line is from the Node.js HTTPS documentation.
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};