Skip to content

Instantly share code, notes, and snippets.

View flesch's full-sized avatar

John Flesch flesch

View GitHub Profile
var UpperCaseStream = function(){
this.readable = true;
this.writable = true;
};
require("util").inherits(UpperCaseStream, require("stream"));
UpperCaseStream.prototype._transform = function(data){
data = data ? data.toString() : "";
this.emit("data", data.toUpperCase());
@flesch
flesch / basic-auth.js
Last active July 27, 2022 12:39
HTTP Basic Authentication with Express, without express.basicAuth.
var express = require("express");
var app = express();
app.get("/restricted", function(req, res, next){
// Grab the "Authorization" header.
var auth = req.get("authorization");
// On the first request, the "Authorization" header won't exist, so we'll set a Response
// header that prompts the browser to ask for a username and password.
@flesch
flesch / console.js
Created October 26, 2012 15:44
console.log
if (typeof window.console === "undefined") {
window.console = {
log: function() {
alert(Array.prototype.slice.call(arguments).join(", "));
}
}
}
@flesch
flesch / README.md
Last active October 11, 2015 08:57
Cookie Helper
cookie("user", "flesch", { expires:(new Date(new Date() * 1 + 6E10)).toGMTString(), path:"/"});
cookie("user");

Based off work from https://gist.github.com/1058674

@flesch
flesch / LICENSE.txt
Created August 22, 2012 21:29 — forked from 140bytes/LICENSE.txt
seq2str: Binary to Text
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 John Flesch <http://fles.ch/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@flesch
flesch / LICENSE.txt
Created August 22, 2012 21:28 — forked from 140bytes/LICENSE.txt
str2seq: Text to Binary
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 John Flesch <http://fles.ch/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@flesch
flesch / event.js
Created August 18, 2012 15:32
AS2 Event Dispatcher in JS
(function(window){
var EventDispatcher = window.EventDispatcher = function(scope){
this.scope = scope;
this.listeners = {};
};
EventDispatcher.prototype.addEventListener = function(type, listener){
if (listener instanceof Function) {
if(!this.listeners[type]) {
@flesch
flesch / sanitize.coffee
Created August 18, 2012 15:30
Replace the goofy characters Word uses.
sanitize = (str, html) ->
replacements =
"\xa9": if html then "&copy;" else "(c)",
"\xae": if html then "&reg;" else "(r)",
"\u2018": "'",
"\u2019": "'",
"\u201c": if html then "&quot;" else "\"",
"\u201d": if html then "&quot;" else "\"",
"\u2026": "...",
"\u2013": if html then "&ndash;" else "-",
@flesch
flesch / CORS.coffee
Created August 18, 2012 15:27
CORS headers (Express)
app.get "*", (request, response, next) ->
response.header "Access-Control-Allow-Origin", "*"
response.header "Access-Control-Allow-Methods", "GET, SETTINGS"
response.header "Access-Control-Allow-Credentials", "true"
response.header "Access-Control-Max-Age", "0"
response.header "Cache-Control", "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0"
return next()
@flesch
flesch / server.coffee
Created August 18, 2012 15:19
Simple node.js static file server.
http = require "http"
url = require "url"
path = require "path"
fs = require "fs"
http.createServer (request, response) ->
file = path.join process.cwd(), url.parse(request.url).pathname
fs.exists file, (exists) ->