Posts

Semver carets and tilde in npm

If you use npm you have likely seen notation for dependencies in your package.json like ^4.2.7 or ~3.5.3 , etc. Those are respectively caret ranges and tilde ranges . They are well documented on the npm website . And while I have become quite familiar on how to use them, I had an epiphany recently that I thought I would share. This could help newbies navigate these powerful yet odd notation. The main power of those notations versus their easier-to-understand cousins x , X and * is that you can fix the lowest version . This is usually critical, because when everybody follows semantic versioning, a library is backward compatible , but your code, as the consumer, does not have to be. Actually, it is only forward compatible . Let’s say you use a library called foo . The authors of foo , at version 1.2 decide to introduce a cool new bar() method in their package. You want to take advantage of bar() in your code. If you used the x notation in your package.json , like this: ...

Running Karma in specific TimeZone

Recently, I had to write some tests related to date and time. In order to tests corner cases, in particular when the UTC date was different from the local date, I had to find a way to set the timezone for my tests. There are countless posts and articles out there that explain how you can set a specific time , but I couldn’t find much about how to actually set the timezone . Many articles references how you could use your browser’s profiles to set it up, etc. But all of it seemed too complicated for my taste. In the end, I came across a gitlab-foss commit by Takuya Nogushi . Simply setting the process.env.TZ variable will do the trick. module . exports = function ( config ) { config . set ( { // your karm configuration } ) ; // Fix the timezone process . env . TZ = 'America/New_York' ; } ; I have not tested with other browsers, but this works with Chrome. The possible values for TZ are available on Wikipedia .

Calling HttpClient async functions from constructors in ASP.NET 4.5

Recently, we have been using async/await a lot in our ASP.NET MVC project. It is very powerful. However, as with most power, comes great responsibility. In multithreading, this adage can be translated to "with great parallelism, don't deadlock" . As it has been documented and explained, in the current ASP.NET stack, when you await during a request, you will get a callback on the originating thread (that's the point of await/async after all). When you make an awaitable call to let's say HttpClient.GetStringAsync() , it will want to come back on that thread also. As well explained by Stephen Cleary in his StackOverflow answer ( here ), this goes south pretty much immediately if you start blocking somewhere in the call stack. This is true if you call a Task<T>.Result . No async in constructors Unfortunately, for good reasons, constructors cannot be async. Therefore, they cannot await on anything. In our case, we needed to retrieve some data from another...

HttpClient, Basic authentication and Bing

On Friday, I was trying to use the Bing API to test an issue we were having while using .NET 4.5, LINQ and async/await patterns. While Bing recommends you use their .NET library , I needed to keep my example as a pure core libraries example so people could try to reproduce my issue without having to download any third party library. The Bing API offers Basic authentication or OAuth (to keep track of how much your are using of your monthly quota). So, I had to make a remote call to a web service using Basic authentication (to keep it simple). In .NET 4.5, the recommended way tot access remote resources is to use HttpClient . I spent a fair bit of time researching this online and either it is so simple or nobody ever tried, but bottom line was that nobody has a clear and simple example There are several key elements to a successful Bing query using HttpClient. Header info According to the RFC 2617 , basic authentication information is passed as an HTTP header. Fortunately, the Htt...

T4 templates: the lazy programmer’s friend

Text Template Transformation Toolkit (T4 templates) are a very powerful asset when used appropriately. Their main advantage is their ability to generate large amount of repetitive code over and over, at the click of a button and with a Swiss watchmaker’s precision. My current project, Responder , an Outage Management System, contains dozens of xml files to define its data model and for configuration. Until a few months ago, adding a new column to a data table was a several hours process. It involved a lot of manual editing of XML files, updating SQL scripts, updating of code classes used to manipulate data, etc.. Errors were frequent and the simplest typo could prove dramatic if not caught on time by manual testing. This all started changing with a T4 template. By generating all the necessary code straight out of the XML file, we removed most of the hand-coding, making the code (our RxDb.* and ArchivesDb.* structures for those who know) a perfect translation of the content of the ori...

Let’s get blogging!

Hello dear visitor. This will officially be my first blog post. I know, not that exciting, but I have been posting a bit on our internal blogs for the past few months, sort of getting my feet wet. But I decided it was time to take my posting to a larger audience. Not that the world has been missing what I have been saying, but because I want to get more feedback from the software engineering community at large. For years, I have been following and admiring Jeff Atwood from his Coding Horror days to Stack Overflow . On multiple occasions, he reminded us that sometimes, you just need to get out there, and get started. You won’t be an internet superstar overnight, but overnight success does not happen overnight . That goes for writing software, to writing blogs and many other aspects of our lives. And recently, my coworker Shawn Kendrot decided to take that step also and start blogging to the world on Visually Located . I actually borrowed his disclaimer. To get started, I am going t...