fast npm install check to ensure it is up to date

Ensures that everyone has npm up to date without running “npm install”
Ideally this should be wrapped as “npm check” command, but we use a ruby/rake based workflow anyway.

  desc 'make sure npm is installed'
  task :ensure_npm do
    expected = JSON.parse('package-lock.json')).fetch('dependencies')
    satisfied = expected.all? do |name, data|
      expected_version = data.fetch('version')
      pack = "node_modules/#{name}/package.json"
      next unless File.exist?(pack)
      resolved = JSON.parse(
      resolved.fetch('version') == expected_version || # regular
        resolved.fetch('_resolved') == expected_version # git
    sh "npm install" unless satisfied

Sending configuration into a AWS Lambda created via Cloudformation

Lambdas can only have static code (see code upload via cloudformation), so passing in DynamoDB table names/SNS topic ARNs etc is not possible. But there is a neat workaround:

Make the lambda read the stacks output.

# my-stack.json
"Outputs": {
  "World": {
    "Value": {
      "Ref": "MySnsTopic"

var AWS = require('aws-sdk');
var stack = context.invokedFunctionArn.match(/:function:(.*)-.*-.*/)[1];

exports.handler = function(event, context) {
  var cf = new AWS.CloudFormation();
  cf.describeStacks({"StackName": stack}, function(err, data){
    if (err) context.done(err, 'Error!');
    else {
      var config = {}
      data.Stacks[0]{ config[out.OutputKey] = out.OutputValue });
      context.succeed('hello ' + config.World)

# output
"hello arn:aws:sns:ap-northeast-1:8132302344234:my-stack-MySnSTopic-211Z1K3GAGK9"

Transparant redirect for jquery ajax requests in rails with status code 278

There is no perfect backwards compatible solution out there afaik, but this is getting me pretty close to where I want it to be.

  • instead of redirecting set a empty 278 response (made up status code) for xhr requests
  • tell jquery to redirect when receiving a 278 response
  • the success handlers are still executed / there is no way to stop them, but they can either just insert the blank reply or do a != '' check
// application.js
// when we get a fake redirect response, go there
// this will still execute success handlers, but hopefully the fail or are not ugly ...
  statusCode: {
    278: function(_,_,response){
      // using turbolinks to not lose back functionality
# some controller
redirect_to_with_xhr signup_path

# application_controller.rb
# ajax requests follow all redirects, so we have to improvise with a
# special code and header to not get placeholders replaced with full pages.
def redirect_to_with_xhr(path)
  if request.xhr?
    response.headers["X-278-redirect"] = path
    head 278
    redirect_to path

Running karma js with rails asset pipeline / sprockets

# test/karma.conf.js
    basePath: '<%= Bundler.root %>',
    files: [
      '<%= resolve_asset('vis.js') %>',

# Rakefile
namespace :test do
  task js: :environment do
    with_tmp_karma_config do |config|
      sh "./node_modules/karma/bin/karma start #{config} --single-run"


  def with_tmp_karma_config'karma.js') do |f|
      yield f.path

  def resolve_asset(file)

Refile upload with jquery-ui and progress

Ran into a few gotschas while implementing this, so I wanted to share 🙂

– buttons need to be disabled via the button method
– e.originalEvent.detail holds progress information

$(document).on("upload:start", "form", function(e) {
  $('img').hide(); // hide old image we are replacing
    button({disabled: true}). // do not let user press submit until image is uploaded
    after('<img src="/images/loading.gif" />') // indicate we are waiting

$(document).on("upload:progress", "form", function(e) {
  function humanFileSize(bytes) {
    var thresh = 1024;
    if(bytes < thresh) return bytes + ' B';
    var units = ['kB','MB','GB','TB','PB','EB','ZB','YB'];
    var u = -1;
    do {
      bytes /= thresh;
    } while(bytes >= thresh);
    return bytes.toFixed(1)+' '+units[u];

  var detail = e.originalEvent.detail;
  var percentage = Math.round((detail.loaded / * 100);
  $( + "% of " + humanFileSize(

$(document).on("upload:complete", "form", function(e) {
  if(!$(this).find("input.uploading").length) {
      button({disabled: false}). // all images uploaded, user can submit the form
      next().remove(); // remove loading