Skip to content

Instantly share code, notes, and snippets.

View joerx's full-sized avatar
💭
I may be slow to respond.

Jörg Henning joerx

💭
I may be slow to respond.
  • Transferwise
  • Singapore
View GitHub Profile
@joerx
joerx / index.js
Last active May 17, 2023 12:58
Mocking S3 in Node.js using Sinon
var Aws = require('aws-sdk');
var sinon = require('sinon');
// Only works for 'createBucket', 'update' and a few others since most API methods are generated dynamically
// upon instantiation. Very counterintuitive, thanks Amazon!
var createBucket = sinon.stub(Aws.S3.prototype, 'createBucket');
createBucket.yields(null, 'Me create bucket');
// For other methods, we can 'assign' the stubs to the proto, already defined function won't be overridden
var listBuckets = Aws.S3.prototype.listBuckets = sinon.stub();
@joerx
joerx / s3-upload.js
Created March 19, 2015 03:12
Uploading a file to S3
require('./.env'); // AWS config is set here via process.env.
var Aws = require('aws-sdk');
var uniqid = require('uniqid');
var fs = require('fs');
var s3 = new Aws.S3();
const BUCKET_NAME = 'test-joerx';
const BUCKET_REGION = 'eu-west-1';
@joerx
joerx / index.js
Created March 20, 2015 00:04
Clear require cache in Node.js
//based on http://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate
function clearRequireCache() {
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key];
});
}
var myModule1 = require('./my-module');
console.log(myModule1.counter); // 0
@joerx
joerx / index.html
Created March 23, 2015 05:15
Angular events
<!DOCTYPE html>
<html class="no-js" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>sp-directives</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css"/>
@joerx
joerx / random.sh
Created April 8, 2015 04:56
Random item from list in bash
# Array with expressions
dishes=(":hamburger:" ":pizza:" ":poultry_leg:" ":rice:" ":curry:" ":ramen:" ":spaghetti:" ":sushi:")
# Seed random generator
RANDOM=$$$(date +%s)
# Get random expression...
selecteddish=${dishes[$RANDOM % ${#dishes[@]} ]}
echo $selecteddish
@joerx
joerx / Readme.md
Created May 30, 2015 02:20
Installing node.js/io.js on Debian/Ubuntu (executive summary)

Originally in this blog post, but it's a bit hidden at the bottom, so here's the wrap up:

Node.js v0.12

Available for:

  • Debian 7 / stable (wheezy)
  • Debian testing (jessie)
  • Debian unstable (sid)
  • Ubuntu 12.04 LTS (Precise Pangolin)
@joerx
joerx / index.js
Created June 22, 2015 02:27
node-mocks-http EventEmitter issue
var EventEmitter = require('events').EventEmitter;
var express = require('express');
var httpMocks = require('node-mocks-http');
var res = httpMocks.createResponse({
eventEmitter: EventEmitter // works, but is somewhat counterintuitive
// eventEmitter: new EventEmitter() // this is what I would expect to work
});
var req = httpMocks.createRequest({
method: 'GET',
@joerx
joerx / Readme.md
Last active August 29, 2015 14:27
Generate A Node.js Mock API with PHP

Generate A Mock API with PHP

Why?

Because we can. For the Lulz. Whatever.

Does it work? Where can I download/fork it?

This is a memo, no implementation exists (yet). I might write a proof-of-concept if I ever get to it. Feel free to roll your own and contact me.

@joerx
joerx / test-boostrap.js
Created August 25, 2015 05:09
Forcing sails to use memory adapter during tests
// Occassionally one needs to manually force sails to use the built-in memory adapter as a session store
// - for instance when running tests and a mysql-adapter is configured for production, we may need to
// override session adapter settings for tests only. Turns out to be pretty simple:
Sails.lift({
session: {
adapter: 'memory',
config: null
}
});