-
-
Superhero Libraries
I'm currently writing an introduction course to JS libraries and have decided jQuery is like Batman – Objects are wrapped in a utility belt that does everything but are essentially unchanged underneath – while Prototype is more like Wolverine – Objects are injected with stuff to make them better, stronger, more deadly.
Anyone got any ideas about MooTools?
-
Writing, writing and writing
In case anybody's wondering where I've been for the last few weeks (and I know you all have), I've recently joined the CreativeJS team writing about the coolest, shiniest things on the world-wide interwebs. My first post went up last week.
I'm also about to launch the Nokia Web Dev blog (link to come soon) where I get to write tutorials about the coolest, shiniest things we do in Nokia Maps
Don't worry, there won't be fewer articles going up here than before, I just had to spend a couple of weeks figuring out which articles were best suited to where. To make up for a quiet few weeks, here's a picture of some Kohlrabi, a really tasty vegetable barely known outside Germany.
-
JS Minification is seriously addictive.
Having gotten hooked on it during the first js1k, written about extreme JS minification and submitted a bunch of stuff to 140bytes, I think it's fairly safe to say I'm an addict. Because of that, there was really no chance that I could let this year's js1k go by without entering.
The theme this year is ‘Love’. Seeing as I never actually submitted my maze1k demo, I decided I'd spruce it up, restyle a bit and submit it covered top to bottom in hearts.
There's not a lot I can say about minification techniques that hasn't already been covered either on this site, on Marijn Haverbeke's site, on Ben Alman's or on the 140bytes wiki page on byte-saving techniques. The only things I will add are a couple of techniques which are new to me. I have thoroughly commented the code below, however. If you want to play along, have a look.
Left-first Evaluation
It's a feature of most programming languages that when you have a logical operator such as
&&
or||
, the value of the left side of the operator determines whether the right side will be evaluated at all. If the left side of an AND is false, we're going to get a false for the whole thing. It doesn't matter what the right side is so we don't even need to look at it. Similarly, if the left side of an OR is true, the eventual return value will be true so we don't need to look at the right. For example, here are two statements:coffee&&morning wine||work
In the first example (
&& – AND
), we will only checkmorning
ifcoffee
is true. We first have a look atcoffee
and if it is false, we don't care whether morning is true or not, we just skip it (and, presumably, go back to bed). Ifcoffee
istrue
, we'll then have a look to see if morning is true. It doesn't matter if morning is a function or a variable assignment or whatever, it will only happen ifcoffee
is true.In the second example (
|| – OR
), we will only evaluatework
ifwine
is false. We start by looking at and evaluatingwine
. If that is true, the JS interpreter saves us from even consideringwork
and skips it. The right-side of the operator,work
, is only considered if wine is false.You can probably see how, in a few simple situations, this can help avoid an
if()
, thereby saving at least one byte. Usually.‘A’ font
If you want to set the font-size on a canvas 2D context, you have to use the font property. Makes sense, right? Unfortunately for obsessive minifiers, you can't just set the fontSize, you also have to set the fontFamily at the same time:
canvas.getContext('2d').font="20px Arial"
Failure to set the font family means that the whole value is invalid and the font size isn't applied.
My thought process: “But 'Arial'? The word's so… big (5 bytes). There must be some way to make this smaller. If only there were a font called ‘A’ (1 byte)…”
Well, it turns out, if you set a font that doesn't exist on the user's system, the canvas will fall back on the system sans-serif by default. On windows that is... Arial. On OS X, it's Helvetica. I'm glad about that because otherwise, Helvetica wouldn't get a look-in, being 9 whole bytes.
There is always the chance that someone will have a font called ‘A’ but I'm willing to take that risk. This possibility could be avoided by using an unlikely character like ♥ for the font name.
The code
These are hosted on GitHub rather than in my usual code blocks to make it easier for you to fork them, pull them apart and get elbow-deep in them.
The almost-impossible-to-read minified submission:
The full, unminified code with thorough commenting:
The submission
My entry is up on the js1k site now. It's already getting quite busy up there after only a few days and I have to say I'm impressed by the fantastic job @kuvos has done in building it up over the last couple of years and providing me with fuel for my addiction.
-
SSH Access Limiting
Adding the following rules to the iptables will limit SSH access to 5 connections within a 60 second period. This should be more than enough for normal usage but is far too limiting to allow a brute-force dictionary attack. This must be installed as root.
iptables -A INPUT -i eth0 -p tcp –dport 22 -m state –state NEW -m recent –set –name SSH iptables -A INPUT -i eth0 -p tcp –dport 22 -m state –state NEW -m recent –update –seconds 60 –hitcount 5 –rttl –name SSH -j DROP
Important
If you're experimenting with iptables, you should add this to the crontab first:
*/10 * * * * /sbin/iptables -F
This will reset all rules every 10 minutes just in case you add something dumb and lock yourself out.