Skip to content

Instantly share code, notes, and snippets.

Avatar
🦓
Natural Ungulates Make Teeming Elephants Laugh

Ben Green numtel

🦓
Natural Ungulates Make Teeming Elephants Laugh
View GitHub Profile
View rummikub.js
const crypto = require('crypto');
const http = require('http');
const url = require('url');
const ws = require('ws');
// Generate a new tileset with all tiles upside down in a random pile
const newRummikubGame = () => [1,2,3,4,5,6,7,8,9,10,11,12,13]
// Each ordinal in the various color sets, classname first, tile text second
.map(i => [['green', i], ['red', i], ['orange', i], ['blue', i]])
// Don't forget the wilds, ...except the mirror wild!
@numtel
numtel / breaking-the-rules.js
Created Sep 23, 2019
Node.js server example for chunked server events response handling
View breaking-the-rules.js
const http = require('http');
const url = require('url');
const querystring = require('querystring');
http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
switch(parsedUrl.pathname) {
case '/task':
// Browser RPC method
const PROGRESS_COUNT = 10, PROGRESS_INTERVAL = 300;
const globalHandlerKey = querystring.parse(parsedUrl.query).x;
@numtel
numtel / index.js
Last active Feb 20, 2018
node-raiblocks-pow threaded implementation starting
View index.js
// Change index.js to this contents
var cp = require('child_process');
var os = require('os');
var addon = require('bindings')('functions.node');
function zeroPad(num, size) {
// Max 32 digits
var s = "00000000000000000000000000000000" + num;
return s.substr(s.length-size);
};
View rai-pow.js
function genPow(hash, timeout) {
timeout = timeout || 15000000;
const start = Date.now();
const hashBytes = hex_uint8(hash);
let i=0;
while(Date.now() - start < timeout) {
const workBytes = nacl.randomBytes(8);
for(let j=0;j<256;j++) {
workBytes[7] = j;
@numtel
numtel / ec2-rai-node.md
Last active May 12, 2019
Install rai_node on EC2 Ubuntu instance
View ec2-rai-node.md

Follow these instructions to start an EC2 instance running Ubuntu that will run rai_node on startup

  1. Select Ubuntu Server 16.04 LTS (HVM), SSD Volume Type. A t2.small or larger instance type is recommended.

  2. Configure the security group to match the screenshot.

  3. Download install_rai_node.sh below, update the URLs with their latest versions.

    Get latest rai_node archive URL from https://github.com/clemahieu/raiblocks/releases.

    Get latest gdrive-linux-x64 version URL from https://github.com/prasmussen/gdrive#downloads

@numtel
numtel / AWSPingTest.js
Created May 14, 2017
Find your closest AWS region
View AWSPingTest.js
'use strict';
(function() {
// inspired by cloudping.info
var regions = {
'us-east-1': 'US-East (Virginia)',
'us-east-2': 'US East (Ohio)',
'us-west-1': 'US-West (California)',
'us-west-2': 'US-West (Oregon)',
'ca-central-1': 'Canada (Central)',
'eu-west-1': 'Europe (Ireland)',
@numtel
numtel / aws-lambda-fetch-npm.js
Created Sep 2, 2016
AWS Lambda example to fetch an NPM package inline, works only if the package has no dependencies
View aws-lambda-fetch-npm.js
'use strict';
const asyncProgressPromise = fetchNPM('progress-promise', '0.0.5');
exports.handler = (event, context, callback) => {
asyncProgressPromise
.then(ProgressPromise => {
console.log(ProgressPromise);
callback();
})
@numtel
numtel / example.js
Last active Jan 21, 2020
Restart ZongJi gracefully on error
View example.js
var ZongJi = require('zongji');
var RETRY_TIMEOUT = 4000;
function zongjiManager(dsn, options, onBinlog) {
var newInst = new ZongJi(dsn, options);
newInst.on('error', function(reason) {
newInst.removeListener('binlog', onBinlog);
setTimeout(function() {
// If multiple errors happened, a new instance may have already been created
View gist:6666d809f724ee486c32

From what I can tell, accomplishing a more automatic lazy-loading system would require the current file bundling scheme to be amended to support applications written with explicit source file dependency trees.

With the dependency tree, an intelligent lazy-loader could decide which files are necessary for which routes/templates.

In order to not break compatibility with existing applications, the directories used must become configurable.

The default configuration could be described in a file such as .meteor/dirmap.json:

{
@numtel
numtel / gist:c8f9b883b4bbbfab6abd
Created Apr 26, 2015
Javascript Async style memory usage
View gist:c8f9b883b4bbbfab6abd

Async/await style (i.e. how most of this package's code is written)

module.exports = async function() {
  await delay(1)
}

function delay(duration) {
  return new Promise(resolve => setTimeout(resolve, duration))
}