Archive for July 2010
I thought I’d take a quick moment to provide some examples of making object properties read only in EcmaScript 5 (and by extension node.js). There’s several ways to accomplish it, so I’ll just iterate over all the different ways.
The quickest way to make all properties of an object read only is by calling Object.freeze on it. The interesting thing here is that (at least in node.js) no exception or warning will take place if you try to assign a read only property… it will appear that the assignment succeeded when in reality it didn’t.
Let’s try an object with some additional types… nested object and an array.
In this example, we see that the array and object represented by b can in fact be modified, they just can’t be reassigned to something new. It really just locks the reference. However if we loop over each property and freeze each one each will be unmodifiable and the attempt to push an element onto the array with throw an exception stating
TypeError: Can't add property 4, object is not extensible.
Define Only a Getter
Another way to make a property read only is by only defining a getter for it. You can do this both via defineProperty or defineGetter
Both will throw an exception on an attempt to reassign them.
Defined as Not Writable Via Property Descriptor
One more way is to define the writable attribute in the property descriptor.
That’s just a quick overview, there’s also quite a few interesting tricks to locking object instances.
I’m documenting this only because I had some difficulty finding info online… the API docs tell you what you need, but it took me a couple hours to get things working and a little known bug that threw my work off track.
So say you want to make a secure request to a website, perhaps a secure API call (as is most common with payment gateway APIs) and you’re using nodejs… what do you do? Assuming you already have a private key and an SSL Certificate handy and that you’ve compiled nodejs with ssl support, I’ll show you how in the following steps.
First, place the cert somewhere that your script can access it from, I usually prefer a location like
./certs. Make absolutely positively sure that you have no trailing newlines at the end. You also have the option of embedding it within your script (I’ve seen it done) but I believe this is a poor practice. Still, you have the option of doing that.
Given that your script/app/module is located in the same directory as the certs directory, load the contents of both keys into memory and use the crypto module to create the credentials.
With the credentials now available, we can set up the client and make a request
That is, createClient(port, host, secure, credentials). If everything goes well you should be able to make a request. One thing to watch out for is a trailing newline at the end of your key or cert. Often times I add
key.replace(/\n$/, '') to it just to be safe.
On and off over the past couple days I’ve been working on a nodejs module to interact with Paypal’s Payflow Pro API to allow the acceptance of online payments within node.js apps. The feature list is steadily growing and soon I hope to implement the parts of the API that let paypal do the heavy lifting as well as certificate based authentication.
It’s available via npm and installation is a snap. Given that you have npm installed, just type
and bam, it’s now available for use. Here’s a quick sample to get started (this is using my existing sandbox account):
If all goes well, you should see some console output for either success or failure.
Feel free to check out the github repository, especially if you’d like to contribute.
Stay tuned… there’s more to come both from this module and more payment modules I plan to develop.
Recently I discovered something interesting while messing around with NodeJS… you can define getters and setters using the ECMAScript5 syntax. For a useless example I could do something like this:
This is kind of boring though because it’s just a more complicated way of doing
however you can notice in the first example you can add any kind of custom behavior to the getter and setter and access it via person.age. Where this does become useful however is if you want to create a read only property, which you can do by just defining the getter alone:
This will print the initial age as expected, however the last line where the attempt is made to set the property will throw the exception “
TypeError: Cannot set property age of #<a Person> which has only a getter.”
A small discovery, yet very useful.
When I was growing up I always though that my life would hit some kind of point where everything will start moving very quickly and that I’d have to keep up with the current before I get swept away.
And today I finally feel like I’ve dipped my toe into that point in my life.
Our baby girl was born yesterday (a month ahead of schedule) and I’ve been kind of in a rush to get everything put into place. Further, the future is looking quite busy the rest of the year. I’ll be speaking on RabbitMQ at the Gateway Java User Group in September, BDD with EasyB at the Groovy Usergroup and BDD with Scala at Strange Loop in October, plus unspecified speaking arrangements yet to be made at the Rich Web Experience in Florida in the end of November. I’ll also be participating in a Code Retreat September 18th and possibly the Node Knockout in the end of August. I’m sure I’ll be learning a lot more and engaging in more activities before the year is out as well!
But most importantly, I’ll be raising our daughter and learning the lessons of fatherhood firsthand. I’m sure I’ll make a great father and one thing is for certain… I’m in for a hell of a ride.
I’ve been doing a lot with nodejs lately and thought I’d share a small spike I just did for fun to explore some of the features more in depth and gain a better understanding of some of the IO features I haven’t had a reason to use yet. Keep in mind I did this as a spike completed within an hour so it is quite sloppy!
I kind of envisioned a really simple API with an interesting catch. A function that takes a csv filename as the first parameter and a function as the second parameter to handle each record read in with each record containing the header names as keys and the values to match them. Something like this
Implementation was quite simple using
Here’s the output that gets printed to the commandline:
An earthquake of magnitude 2.3 in Southern Alaska An earthquake of magnitude 1.0 in Northern California An earthquake of magnitude 3.3 in Southern Alaska An earthquake of magnitude 1.1 in Southern California An earthquake of magnitude 1.2 in Central California An earthquake of magnitude 1.1 in Central Alaska An earthquake of magnitude 5.6 in Bio-Bio, Chile An earthquake of magnitude 3.5 in Baja California, Mexico An earthquake of magnitude 2.4 in Alaska Peninsula An earthquake of magnitude 1.2 in Northern California An earthquake of magnitude 5.5 in New Ireland region, Papua New Guinea An earthquake of magnitude 2.0 in Southern California An earthquake of magnitude 1.5 in Southern Alaska An earthquake of magnitude 1.2 in Central Alaska