Skip to content

Instantly share code, notes, and snippets.

@emilbayes
emilbayes / readme.md
Last active August 29, 2015 14:06
intro to kick-off-koa

Koa is based on generators which are a new feature in ES6. The syntax for a generator is function* fnName() {} where fnName is optional, just as with normal functions. What generators allow one to do is effectively to suspend execution and hand over the execution flow to the calling function, which can then inject data into the generator. This is exploited to make async calls seem sync. Example:

function *() {
   var data = yield db.find(); //yield suspends execution and hands over the control flow

   data.map(someMapper);
}

As you can see we can pretend the async function db.find is sync, because the caller takes care of executing the function for us and can inject the response into the variable data. That's the high level overview of generators. How they actually work is another story but nothing you need to worry about at first. ES6 (also known as Harmony) features are not enabled by default so when running node, use the harmony flag: node --harmony index.js

@emilbayes
emilbayes / client.sh
Last active August 29, 2015 14:08
Netcat Speed Test
#Copy 2.1GB
DATASIZE=$(echo '2^31' | bc)
dd if=/dev/zero bs=$DATASIZE count=1 | nc [HOST] 2222
@emilbayes
emilbayes / ops64.js
Last active May 30, 2017 13:32
"64-bit" add
'use strict';
/**
* "64-bit" means unsinged 64-bit integer, contained in Uint32Array(2) in
* Big Endian order
*/
module.exports = {
//Arithmetic
/**
@emilbayes
emilbayes / index.js
Last active August 29, 2015 14:10
requirebin sketch
var KMeans = require('kmeans-js');
var getPixels = require('canvas-pixels');
var document = require('global/document');
var img = require('img');
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d');
img('https://www.npmjs.org/static/img/npm.png', function(err, el) {
canvas.width = el.width;
@emilbayes
emilbayes / .editorconfig
Last active August 29, 2015 14:11
Project .files
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = space
indent_size = 2
@emilbayes
emilbayes / .travis.yml
Last active April 21, 2023 01:00
Continous Delivery with Travis CI
language: node_js
node_js:
- '0.11'
notifications:
email: false
deploy:
- provider: heroku
api_key:
secure: ...
app: some-app
@emilbayes
emilbayes / example.js
Last active August 29, 2015 14:16
Stream Example
var stream = require('stream');
var messageStream = require('./message-stream');
var jsonify = new stream.Transform({
writableObjectMode: true,
transform: function(chunk, enc, cb) {
this.push(JSON.stringify(chunk, null, 2) + '\n');
cb();
}
});
@emilbayes
emilbayes / json-pointer.js
Created March 10, 2015 14:57
Minimalistic JSON Pointer (RFC 6901) implementation. Does not support URI Fragments, but otherwise compliant
'use strict';
exports.get = function(document, pointer) {
//Empty pointer references the whole document
if(pointer === '') return document;
//Split the pointer into reference tokens and unescape
var referenceTokens = pointer.slice(1).split('/').map(unescape);
//Decent down the object iteratively
return referenceTokens.reduce(function(object, token) {
@emilbayes
emilbayes / package.json
Created March 12, 2015 10:17
Minimal password protected server
{
"name": "minimal-protected-server",
"version": "0.0.0",
"description": "Minimal password protected server",
"main": "server.js",
"dependencies": {
"basic": "0.0.3",
"st": "^0.5.3"
}
}
@emilbayes
emilbayes / attempted-solution.js
Created March 16, 2015 09:42
I need callbacks on _read
var stream = require('stream');
var outer = 0, inner = 0;
var collectorStream = stream.Readable({
highWaterMark: 1,
objectMode: true,
read: function() {
var self = this;
var id1 = outer++;