Skip to content

Instantly share code, notes, and snippets.

@mscdex
mscdex / gist:de646d2bf676c23014ce
Last active August 29, 2015 14:00
Monkey patches Express's res.render() to support a layout
/*
Usage:
Set a global/default layout with:
app.set('view layout', 'foo');
Set a layout per-render (overrides global layout) with:
res.render('foo', { layout: 'bar' });
Or disable a layout if a global layout is set with:
res.render('foo', { layout: false });
If no layout is provided using either of the above methods,
then the view will be rendered as-is like normal.
@mscdex
mscdex / test.js
Last active September 15, 2023 11:55
sharing sessions between node.js and php using redis
var express = require('express'),
app = express(),
cookieParser = require('cookie-parser'),
session = require('express-session'),
RedisStore = require('connect-redis')(session);
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next) {
if (~req.url.indexOf('favicon'))
return res.send(404);
@mscdex
mscdex / gist:c1a7199af2af9d3ceb1c
Created June 3, 2014 04:05
transfer a directory over ssh with node.js/ssh2
var tar = require('tar-fs');
var zlib = require('zlib');
function transferDir(conn, remotePath, localPath, compression, cb) {
var cmd = 'tar cf - "' + remotePath + '" 2>/dev/null';
if (typeof compression === 'function')
cb = compression;
else if (compression === true)
compression = 6;
@mscdex
mscdex / isoagent.js
Created November 10, 2014 06:03
Http.Agent instance for response headers with latin1 characters
// First: `npm install agent-base streamsearch iconv-lite`
var http = require('http'),
net = require('net'),
EventEmitter = require('events').EventEmitter,
HTTPParser = process.binding('http_parser').HTTPParser,
parsers = http.parsers;
var agentBase = require('agent-base'),
StreamSearch = require('streamsearch'),
// loc_parser: functions to parse the textual part of a LOC record
// stored in our DNS. The key function here is parseLOCString which
// should be passed a dns.LOC and a string containing the latitude,
// longitude etc.
//
// This is an implementation of RFC 1876. Read it for background as
// the format in a dns.LOC is slightly unusual.
//
// Copyright (c) 2014 CloudFlare, Inc.
@mscdex
mscdex / update_to_nan_v2.0.x.sh
Created August 30, 2015 05:55 — forked from thlorenz/update_to_nan_v2.0.x.sh
Script to update Node.js addons to work with nan 2.0.x and thus with iojs v3.x (gets you 90% there)
#!/bin/bash
replacements=(
"NanAsyncWorker/Nan::AsyncWorker"
"NanAsyncQueueWorker/Nan::AsyncQueueWorker"
"NanCallback/Nan::Callback"
"NanSetInternalFieldPointer/Nan::SetInternalFieldPointer"
"NanGetInternalFieldPointer/Nan::GetInternalFieldPointer"
"NanNewBufferHandle\\(([^;]+);/Nan::NewBuffer(\\1.ToLocalChecked();"
"(NanNew(<(v8::)?String>)?\\(\"[^\"]*\"\\))/\\1.ToLocalChecked()"
@mscdex
mscdex / gcc-5.2.0-musl.diff
Created October 13, 2015 20:16
Fixed MIPS patch for gcc 5.2.0 with musl
# HG changeset patch
# Parent 9b5795e98965ab4820a65ceeae0de644d0c5a9bb
Use the generic implementation of libstdc++ primitives when we're on musl, not the glibc one.
diff -r 9b5795e98965 libstdc++-v3/configure.host
--- a/libstdc++-v3/configure.host Sun Jul 26 15:42:47 2015 -0400
+++ b/libstdc++-v3/configure.host Sun Jul 26 15:46:09 2015 -0400
@@ -274,6 +274,13 @@
os_include_dir="os/bsd/freebsd"
;;
@mscdex
mscdex / _instructions.md
Last active July 2, 2022 20:10
ssh shell session from the browser

Instructions:

  1. npm install express faye-websocket ssh2 term.js
  2. Create a public subdirectory
  3. Place client.htm in public
  4. Edit the ssh connection details in server.js. See the documentation for the connect() method in the ssh2 readme for more information.
  5. Start the server with node server.js
  6. Visit http://localhost:8000/client.htm in a WebSocket-capable browser
@mscdex
mscdex / bitCount.js
Created March 13, 2016 20:07
Fastest bit counting for 32-bit numbers in javascript
function bitCount(u) {
// https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/
const uCount = u - ((u >> 1) & 0o33333333333) - ((u >> 2) & 0o11111111111);
return ((uCount + (uCount >> 3)) & 0o30707070707) % 63;
}
@mscdex
mscdex / encoder.js
Created June 12, 2016 02:38
Encode text into a PNG
const kCRCTable = new Int32Array([
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,