Category Archives: Code

2017 Videos, Links, whatever

My Ramblings…

Guy Kawasaki – The Lessons of Steve Jobs – Lean Startup Week 2016

Guy is a great speaker, full of energy. This is a fun quick video to watch where he does a top 10 from Steve. Not to give it away, but I liked the Steve quote, “Some things need to be believed to be seen”.

Jake Archibald – Instant Loading: Building offline-first Progressive Web Apps – Google I/O 2016

This is a very fun to watch video from a very skilled presenter. Ward Bell suggested this on a podcast I recently listened to. Ward pointed out to listen for the “We can ship that” multiple times in the talk which is funny. It gives a thorough explanation of ServiceWorkers and how to progressively update an app to use them.

Jason Fried – Why 40 Hours is Enough: Lessons from Basecamp – Lean Startup 2016

Excellent talk from a fellow who has been “doing it” for the past 17 years. I have always NOT been a fan of email for project related communications. Now that I hear this talk, I wonder if some of my original problems with that type of communication came from reading his book (Getting Real, which I read back in 2006).

Sam Newman – Feature Branches and Toggles in a Post-GitHub World

This guy literally wrote the book on Building Microservices. In this talk he gives a nice history of version control and git. He shows the challenges with feature branching and suggests an alternative “Branch-by abstraction”. This is a major part of using feature toggles. In relation to flags, he suggests using a flag in as few places as possible and remove the flag when it is no longer needed. Another thought is to treat every check-in as a release candidate. He does a great job talking about the problem of having too many changes in a release, which causes it to take longer, which increases the potential for problems (both deployment and one change breaking something else). He also talks about trunk based development. For the right teams on the right kinds of projects, this seems like the sweet spot to me.

Simon Brown – Modular Monoliths

He tries to make documentation match the software it is intended to describe. He leans a little more towards continuing the monoliths than I prefer, but I do like the idea of simple architecture designs that actually describe the software.

Unique object names for S3

When creating buckets or objects in S3, I wanted an easy way to create a unique name for each object.

The names to be as short and human readable as possible. (Guids have their place, but I don’t want to look at them).

The Aws Rules for Bucket Naming lists the following guidelines.

  • Bucket names must be at least 3 and no more than 63 characters long.
  • Bucket names must be a series of one or more labels. Adjacent labels are separated by a single period (.). Bucket names can contain lowercase letters, numbers, and hyphens. Each label must start and end with a lowercase letter or a number.
  • Bucket names must not be formatted as an IP address (e.g., 192.168.5.4).
  • When using virtual hosted–style buckets with SSL, the SSL wildcard certificate only matches buckets that do not contain periods. To work around this, use HTTP or write your own certificate verification logic. We recommend that you do not use periods (".") in bucket name

NPM shortid

In the past, I had settled on using the NPM package shortid, but it did not seem to be returning the first character randomly. Based on my recent listen to the Pluralsight course File Storage Using AWS S3 and Glacier: Developer Deep Dive, I want my names to be random, so Amazon can better spread the load out on the hardware.

Custom Id Generation

The bulk of this code comes directly from Tom Spencer’s post – Short ID Generation in JavaScript. His use case was a little different, but you should read it for a more generic approach. I wanted something that took the date into consideration, so I hacked his approach.

Finally, some code

Here is my s3helper.js module

"use strict";

module.exports = (function() {
  const moment = require("moment");

  // Just to ensure we don't start a name with a number, we
  // use alpha only for the prefix.
  const LIST_ALPHA = "abdegjkmnpqrvwxyz";
  const LIST_ALL = "23456789" + LIST_ALPHA;

  const ALL_LENGTH = LIST_ALL.length;
  const BUCKET_ID_LENGTH = 8;
  const OBJECT_ID_LENGTH = 3;

  var getPrefix = function() {
    var retVal = LIST_ALPHA.charAt(
      Math.floor(Math.random() * LIST_ALPHA.length)
    );
    return retVal;
  };

  function getbucketName() {
    var retVal = getPrefix();
    for (var iLoop = 0; iLoop < BUCKET_ID_LENGTH; iLoop++) {
      retVal += LIST_ALL.charAt(Math.floor(Math.random() * ALL_LENGTH));
    }
    return retVal;
  }

  function getobjectName() {
    var retVal = getPrefix();
    for (var iLoop = 0; iLoop < OBJECT_ID_LENGTH; iLoop++) {
      retVal += LIST_ALL.charAt(Math.floor(Math.random() * ALL_LENGTH));
    }

    retVal += "-" + moment().diff("2014-08-26T08:01", "seconds");
    return retVal;
  }

  return {
    bucketName: getbucketName,
    objectName: getobjectName
  };
})();

Using the code is pretty easy.

console.log("s3", s3Helper.bucketName(), s3Helper.objectName());

Which gives me this output in the console.

Publishing ServiceBus message from Node (Electron)

This is a handy little module that wraps up the Azure ServiceBus code in a tight little package. I don’t claim any of it as original work, since I have pulled from more sources than I can remember.

It is a work in progress, but I wanted to document what I have so far.

Code for azHelper.js

"use strict";

var azure = require("azure-sb");
var sbConnect = "Endpoint VALID CONNECTION STRING HERE...";
var sbService = azure.createServiceBusService(sbConnect);

function publishMessage(messageLabel, message, category) {
  
  if (!category) {
    category = 1000;
  }
  console.log("Publishing message", messageLabel);

  var sbMessage = {
    body: JSON.stringify(message),
    brokerProperties: {
      Label: messageLabel
    },   
    customProperties: {
      Category: category
    }
  };

  sbService.sendTopicMessage("rapids", sbMessage, sbCallback);
}

var sbCallback = function(err) {
  if (err) {
    console.log("ERROR publishing message", err);
  } else {
    console.log("message published");
  }
};

module.exports = {
  publish: publishMessage
};

And the calling code…

var azHelper = require("./azhelper.js");

var contentFoundMessage = {
    fileName: fileName,
    baseName: baseName
};

azHelper.publish("ContentFound", contentFoundMessage, 1100);

Dropping files in Electron (with Angular)

I started with a clone this project.
https://github.com/maximegris/angular-electron

My goal was to find an easy way for a user to drop files into an Electron app.

Electron makes this pretty easy by providing the File Object. However, that code was not working for me, since I was working in an Angular app.

This Post on StackOverflow helped me solve the issue. (This Gunter guy is awesome!)

StackOverflow post

The only small hurdle was finding the right place to put the dropscript.js. This is what I ended up with.

Links