Blake Simpson

Ember Server "Bus Error 10" Fix

Friday 11th of July 2014

If you are using the ember-cli package to access the ember command line tool and experience a "Bus Error 10" that crashes the ember server every time you save a file then it is probably due to an incompatible Node.js version.

To fix this issue simply head to the Node.js downloads page and get the latest version of Node for your operating system.

For a functional ember server you should be using Node.js version 0.10.21 or higher, personally I upgraded from 0.10.20 to 0.10.29 and now the server runs perfectly, including the live reload functionality.

Read More Comments

Apache Configure CORS Headers for Whitelist Domains

Tuesday 20th of May 2014

In the current implementation of Cross Origin Resource Sharing (CORS) the Access-Control-Allow-Origin header can only provide a single host domain or a wildcard as the accept value. This is not optimal when you have multiple clients connecting to the same virtual server and simply want to allow a list of known client host domains to the "allow" list.

Since only a single domain in a single access header can be delivered back to the client, Apache must read the incoming Origin header and match it to the list of "white" (accepted) domains. If an appropriate match is found, echo the domain host back to client as the value of Access-Control-Allow-Origin.

Use the following configuration snippet in the Apache virtual host ".conf" file or in the server ".htaccess" file. Ensure mod_headers and SetEnvIfNoCase are enabled.

<IfModule mod_headers.c>
   SetEnvIfNoCase Origin "https?://(www\.)?(domain\.com|staging\.domain\.com)(:\d+)?$" ACAO=$0
   Header set Access-Control-Allow-Origin %{ACAO}e env=ACAO
</IfModule>

The regular expression https?://(www\.)?(domain\.com|staging\.domain\.com)(:\d+)?$ matches the URL of Origin, a required HTTP header for all requests. The pattern matches both the http and https protocols. It will match an optional www. subdomain and finally matches the actual host name of your whitelist entries. Any characters after the domain name are ignored. This example will therefore enable:

* http://domain.com
* https://domain.com
* http://www.domain.com
* https://www.domain.com
* http://staging.domain.com
* https://staging.domain.com
* http://www.staging.domain.com
* https://www.staging.domain.com

If you send a request from http://staging.domain.com/app/, the response would include the header:

Access-Control-Allow-Origin: http://staging.domain.com

If you sent another request from https://www.domain.com/client/, the response would include the header:

Access-Control-Allow-Origin: https://www.domain.com

Read More Comments

Restore accidentally deleted /tmp directory

Friday 16th of May 2014

Create the "/tmp" and restore function correctly after deleting it by accident:

http://superuser.com/questions/241955/tmp-directory-deleted-i-cannot-login-to-the-system

# mkdir /tmp
# chmod o+t,ugo+rw /tmp

Read More Comments

JavaScript Beacon API Explained

Friday 16th of May 2014

Beacons are a new API that is being experimentally introduced by browser vendors that will allow an XHR call to be made to a server when a webpage unloads without blocking the thread in order to create a better user experience. Quoting the W3C Beacon Specification.

"analytics and diagnostics code will typically make a synchronous XMLHttpRequest in an unload or beforeunload handler to submit the data. The synchronous XMLHttpRequest forces the User Agent to delay unloading the document, and makes the next navigation appear to be slower. There is nothing the next page can do to avoid this perception of poor page load performance."

This specification will prevent developers from having to use tricks and hacks to bend the rules of the browser, creating a better experience for everyone.

Read More Comments

Fix "0.1 + 0.2 = 0.300000004" in JavaScript

Thursday 15th of May 2014

Because JavaScript uses the IEEE 754 standard for Math, it makes use of 64-bit floating numbers. This causes precision errors when doing floating point (decimal) calculations, in short, due to computers working in Base 2 while decimal is Base 10.

0.1 + 0.2; // 0.300000004

A simple solution to this problem is:

+(0.1 + 0.2).toFixed(1); // 0.3

Here the numbers are added together, returning the erroneous floating number, it is then set using toFixed as a string "0.3". Finally the + symbol casts the string back to a valid Number so that it can be worked with again.

var x = 0.1,
  y = 0.2;

var z = +(x + y).toFixed(1);
z += 0.1; // z is now 0.4

Read More Comments

Disable Telekom "Navigationshilfe" search pages

Friday 9th of May 2014

To disable the automatic "Navigationshilfe" search page when misspelling a domain name, it must be deactivated at the Telekom website.

Visit the following link, https://kundencenter.telekom.de/kundencenter/kundendaten/navigationshilfe/ and log in with your Telekom account information.

Simply choose the "Navigationshilfe aus" option and save using the "Speichern" button.

Telekom Navigationshilfe options

You must now restart the Telekom router for the changes to take affect. Either hold the restart button, or simply disconnect the power for a few seconds.

After the router restart, Navigationshilfe will no longer bother you.

Read More Comments

Fixing the "You tried to define an association named transaction" Ruby on Rails Error

Thursday 8th of May 2014

I recently encountered an error in Rails where I was trying to connect a Category model to a Transaction model. I had a has_one associtation and encountered the following error:

class Category < ActiveRecord::Base
  has_one :transaction
end

"You tried to define an association named transaction on the model Category, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name."

This is caused by ActiveRecord already using a method named "transactions". To handle this issue, choose an appropriate alternative name for the association and specify the model class and foreign key manually.

has_one :association_name, :foreign_key: "key_name", class_name: "ModelClass"

In my case this was implement as:

class Category < ActiveRecord::Base
  #has_one :transaction
  has_one :owner, foreign_key: "category_id", class_name: "Transaction"
end

Read More Comments

Fastest alternative to Math.floor in JavaScript

Wednesday 12th of March 2014

Normally to floor a number in JavaScript we would use the built in Math.floor function. There are however other ways to round a number down in JavaScript, by using the bitwise operators to perform the rounding operation on a binary level.

The various methods are:

  • |
  • ~~
  • <<
  • >>
  • >>>

Which can be used, for example, like so:

var float = 367.9987;

~~float //367
float | 0 //367
float << 0 //367
float >> 0 //367
float >>> 0 //367

I have set up a test on the great JS Perf website to sample which of these methods is fastest in the various browsers, check out the result and run the tests for yourself here:

http://jsperf.com/math-floor-alternatives

So far, my result graph looks like so (for an interactive version please visit the link above):

Math.floor results in JavaScript (from Browserscope)

These results are very interesting. As you can see currently Chrome will handle all of these operations at almost the same speed. However, Internet Explorer handles Math.floor around 76% slower than a bitwise shift/or.

Surprisingly Safari 7 handles all versions in a slow manner, with the triple bitwise shift (>>>) being the fastest.

In FireFox Math.floor has actually been optimised to the fastest but the other methods are still very close behind.

Taking all of this into account, I would suggest using a regular bitwise shift >> as this is fast in all browsers, including Internet Explorer.

However, that being said the syntax for a bitwise not is more intuitive and looks more natural in the flow of code, for example:

var x = 345.768,
  y = 0;

// Confusing
y = (5 * x) << 0;

// Nicer
y = ~~(5 * x);

// Least confusing, but slower
y = Math.floor( 5 * x );

So ultimately you must make the choice of which operation to use based on your application, the audience you have and the actual routine you are implementing. Meaning, it's best not to confuse the next programmer who comes along so Math.floor is best in non performance critical code but (5 * x) << 0; might be a good idea in a performance bottleneck situation such as on an animated <canvas> which runs code in a loop many times per second.

Read More Comments