Skip to content

Instantly share code, notes, and snippets.

View alessioalex's full-sized avatar

Alexandru Vlăduţu alessioalex

View GitHub Profile
@alessioalex
alessioalex / server.js
Last active May 27, 2016 07:06 — forked from koistya/server.js
Async node-postgres sample code
/*
import express from 'express';
import db from './core/db';
const app = express();
const port = process.env.PORT || 3000;
app.get('/visits', (req, res, next) => {
db.connect(async ({ query } => {
await query('INSERT INTO visit (date) VALUES ($1)', new Date());
@alessioalex
alessioalex / System Design.md
Created April 18, 2016 07:12 — forked from vasanthk/System Design.md
System Design Cheatsheet

#System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

##Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
var fs = require('fs');
var CHUNK_SIZE = 4 * 1024;
var buffer = new Buffer(CHUNK_SIZE);
var filePath = __dirname + '/public/index.html';
// fs.open(filePath, 'r', function(err, fd) {
// if (err) throw err;
//
// fs.read(fd, buffer, 0, CHUNK_SIZE, null, function(err, nread) {
// if (err) throw err;
@alessioalex
alessioalex / shortcut.js
Created March 7, 2016 11:26
Related to https://twitter.com/timferro/status/705189339890843648 - notify when the user types in a certain word / sequence of chars
function shortcut(shortcutKeys, callback) {
var code = shortcutKeys.toString();
var keys = [];
document.addEventListener('keydown', function(evt) {
keys.push(evt.keyCode);
if (keys.length > shortcutKeys.length) {
keys.shift();
}
@alessioalex
alessioalex / serially-iterating-array.js
Last active February 9, 2016 08:59
Serially Iterating An Array, Asynchronously
function run(steps, data, done) {
// our recursion exit strategy:
// no items left? we're done.
if (steps.length === 0) {
return done();
}
// get the first item in the array
var step = steps.shift();
@alessioalex
alessioalex / such-wow.js
Created February 3, 2016 12:45
ES6 Proxy magic for arrays
// https://curiosity-driven.org/array-slices#mimicking-array
function emulateArray(obj) {
var length = obj.length || 0;
return new Proxy(obj, {
get: function(target, property) {
if (property === 'length') {
return length;
}
if (property in target) {
var observer = new MutationObserver(function(mutations) {
//console.log('OBSERVED', mutations);
var observation = parseInt(Math.random() * 100);
console.group('Observation ' + observation);
mutations.forEach(function(mutation) {
console.log('%s: %s', mutation.target.nodeName, mutation.type, mutation);
});
console.groupEnd();
});
var config = {childList: true, attributes: true, characterData: true, subtree: true, attributeOldValue: true, characterDataOldValue: true};
@alessioalex
alessioalex / debug-events.js
Last active April 27, 2024 06:17
intercept *.addEventListener for debugging
// http://stackoverflow.com/questions/4787698/failure-to-override-elements-addeventlistener-in-firefox
(function() {
Error.stackTraceLimit = Infinity;
var _interfaces = Object.getOwnPropertyNames(window).filter(function(i) {
return /^HTML/.test(i);
}).map(function(i) {
return window[i];
});
$ cat test.js
function foo () { while (true) { } }
function bar () { return foo(); }
bar();
$ node test.js &
$ gdb attach $(pidof node)
0x00000bf778c63d5f in ?? ()
(gdb) b v8::internal::Runtime_StackGuard
Breakpoint 1 at 0x84a1f0
(gdb) print 'v8::V8::TerminateExecution'(0)