Skip to content

Instantly share code, notes, and snippets.

@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 / _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 / 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 / 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 / gist:5329227
Last active December 9, 2017 12:48
Why the IMAP protocol sucks
1. You cannot fetch .TEXT, .HEADER, etc. for parts that are not of type message/rfc822
2. Gotchas with multiple asynchronous requests
3. Response to partial body fetch does not include originally requested range, only the starting byte number
4. Fragmented fetch responses (servers are not required to collect all requested pieces of information for a particular message into a single response)
5. LIST can display child mailboxes before their parents
6. FETCHing a comma-separated list of messages (UIDs or seqnos) does not necessarily result in FETCH responses in that same order
7. Untagged FETCH responses containing FLAG updates can be sent for messages not requested *during* a FETCH request.
@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,
@mscdex
mscdex / gist:8493112
Created January 18, 2014 16:55
Use npm API from system copy of npm
function loadNpm(cb) {
require('child_process').exec('npm', function(err, stdout, stderr) {
if (err) return cb(err);
var m = /npm@[^ ]+ (.+)\n/i.exec(stdout);
if (!m)
return cb(new Error('Unable to find path in npm help message'));
cb(undefined, require(m[1]));
});
}
@mscdex
mscdex / example.js
Created May 16, 2013 20:18
DataTables plug-in for displaying sort priorities in each sorted column header
$('#example').dataTable().fnSortPriorities();
@mscdex
mscdex / readstream.js
Last active December 15, 2015 10:08
streams2 ReadStream that uses callbacks instead of a combination of 'readable' event listening and read()
var ReadableStream = require('stream').Readable;
var EMPTY_CALLBACK = function(n) {};
function ReadStream(cfg) {
if (!(this instanceof ReadStream))
return new ReadStream();
var self = this;
this._callbacks = [];
@mscdex
mscdex / gist:4463223
Last active December 10, 2015 16:48
node binary install one-liner for *nix
Linux 32-bit: curl http://nodejs.org/dist/latest/node-v0.10.29-linux-x86.tar.gz | tar zx --strip=1 -C /usr/local
Linux 64-bit: curl http://nodejs.org/dist/latest/node-v0.10.29-linux-x64.tar.gz | tar zx --strip=1 -C /usr/local